0

Tried all the ways to pass the test case but it still shows only one error. I do not know how to rectify the error.

Input: 1534236469
Actual Output: 1056389759
Expected Output: 0

I do not know why my code does not give output 0.

class Solution
{
    public static int reverse(int x)
    {
        boolean flag = false;
        if (x < 0)
        {
            x = 0 - x;
            flag = true;
        }
        int res = 0;
        int p = x;
        while (p > 0)
        {
            int mod = p % 10;
            p = p / 10;
            res = res * 10 + mod;
        }
        if (res > Integer.MAX_VALUE)
        {
            return 0;
        }
        if (flag)
        {
            res = 0 - res;
        }
        return res;
    }

    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        int revinteger = reverse(x);
        System.out.println(revinteger);
    }
}
1
  • Please, describe the problem that your code wants to solve (don't assume we know every leetcode problem), and please, format/indent your code so it gets readable. Commented Jul 15, 2021 at 16:30

4 Answers 4

2

The statement res > Integer.MAX_VALUE will never be true as res is of int datatype. And an int can never be larger than the Integer.MAX_VALUE (MAX_VALUE is itself the max we can store as int).
Use long datatype for reversing the number instead and at the end return the integer value of the result.

Sign up to request clarification or add additional context in comments.

Comments

0

Because long based operations are relatively slow, I would only use a long to do the final check to determine if overflow is about to occur.

It should be obvious that no int will cause overflow if all the digits except the last are reversed. So process the last digit outside of the loop before returning the value. The border case for min is eliminated first to facilitate subsequent processing. Now only Integer.MAX_VALUE is of concern for both positive and negative numbers.

public static int reverse(int v) {
    if (v == Integer.MIN_VALUE) {
        return 0; // guaranteed overflow if reversed.
    }
    int rev = 0; 
    long ovflLimit = Integer.MAX_VALUE;
    int sign = v < 0 ? -1 : 1;
    v *=sign;
    
    while (v > 9) {
        int digit = v % 10;
        rev =  rev * 10 + digit;
        v/=10;
    }
    long ovfl = (long)(rev)*10+v;
    return ovfl > ovflLimit ? 0 : (int)(ovfl)*sign;
}

Comments

0

This problem can be solved in various ways but if we stick to Java and your solution then as it already have been pointed out in some of the answers that by using int the condition if(res > Integer.MAX_VALUE) will never be true. Hence you would not get expected output as 0.

You can put a check before reversing last digit by casting the value in long and check if it's out of int limit. As suggested by WJS.

In case you want to stick to your solution without much change below is the working code for the same.

class Solution {

    public static int reverse(int x) {
        boolean flag = false;
        long input = x;
        if (input < 0) {
            input = 0 - input;
            flag = true;
        }

        long res = 0;
        long p = input;

        while (p > 0) {
            long mod = p % 10;
            p = p / 10;
            res = res * 10 + mod;
        }

        if (res > Integer.MAX_VALUE) {
            return 0;
        }

        if (flag) {
            res = 0 - res;
        }

        return (int) res;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        int revInteger = reverse(x);
        System.out.println(revInteger);
    }
}

Comments

0
if (res > Integer.MAX_VALUE)

If it's the max possible value of the integer, how will it ever be greater than it? You'll have to detect it in another way.

One way would be to use the long data type instead of int. Then, it can become larger than Integer.MAX_VALUE, and that catch will work.

Otherwise, you can probably find another way to catch that case.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.