codekofi
← All problems

Problem 32

Medium

1 · Worked examples

0 / 3

Type an input and write what you think is the output . Each problem is converted to Web Assembly, so any possible input will show the corresponding output. Only correct predictions count.

solution()
returns

2 · Which problem is it?

Locked until you have predicted 3 outputs correctly.

The accepted solution

1int solution(int x) {
2
3 int ans = 0;
4
5 while (x != 0) {
6
7 int digit = x % 10;
8 x = x / 10;
9
10 if (ans > INT_MAX / 10) {
11 return 0;
12 }
13
14 if (ans < INT_MIN / 10) {
15 return 0;
16 }
17
18 if (ans == INT_MAX / 10 && digit > 7) {
19 return 0;
20 }
21
22 if (ans == INT_MIN / 10 && digit < -8) {
23 return 0;
24 }
25
26 ans = ans * 10 + digit;
27 }
28
29 return ans;
30}

Names have been stripped. The signature is the only clue you get for free. Compiled as C++20 with the standard headers and using namespace std; already in scope.