What should be the output for 1534236469?

  • 2

    Expected is 0.
    But the input is much smaller than INT_MAX which is 2147483647

  • 0

    I have the same problem with my solution (in Python), the expected answer is 0 for some reason.

  • 1

    The crucial condition that the output should be 0 if it doesn't fit 32-bit integer was hidden as a spoiler! That should be fixed.

    @practice.pankaj: the answer should be 0 because the correct answer (9646324351) is greater than 2^31.

  • 1
        int temp = x;
        long result = 0;
        do {
            result = result * 10 + temp % 10;
            temp = temp / 10;
        } while(temp);
        if (result > INT_MAX || result < INT_MIN) {
            return 0;
        return result;

  • 1

    Because the result overflows as an integer. The trick is that you can use a 'long' to store the result and judge the overflow situation before return :)

  • 0

    I know now why that happened, although 1) the task to return 0 in such situations was hidden as a spoiler which I generally don't read (spoilers are for hints, not for actual problem definitions), 2) I haven't even used a language in which such overflows can occur. Even if I used C++ I'd rather use long long than return 0.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.