REVERSE INTEGER PROGRAM IN JAVA

By | March 28, 2024

Example 1:
Input: x = 120
Output: 21

public class ReverseInteger {
    public static void main(String[] args) {
        ReverseInteger solution = new ReverseInteger();
        int x = 123;
        System.out.println("Input: " + x);
        System.out.println("Output: " + solution.reverse(x));

        x = -123;
        System.out.println("\nInput: " + x);
        System.out.println("Output: " + solution.reverse(x));

        x = 120;
        System.out.println("\nInput: " + x);
        System.out.println("Output: " + solution.reverse(x));

        x = 0;
        System.out.println("\nInput: " + x);
        System.out.println("Output: " + solution.reverse(x));
    }

    public int reverse(int x) {
        int rev = 0;
        while (x != 0) {
            int digit = x % 10;
            x /= 10;
            if (rev > Integer.MAX_VALUE / 10 || (rev == Integer.MAX_VALUE / 10 && digit > 7)) return 0;
            if (rev < Integer.MIN_VALUE / 10 || (rev == Integer.MIN_VALUE / 10 && digit < -8)) return 0;
            rev = rev * 10 + digit;
        }
        return rev;
    }
}
  1. Initialize a variable rev to store the reversed integer. This will be the return value.
  2. Iterate while x is not equal to 0: a. Get the rightmost digit of x using the modulus operator % and store it in the digit variable. b. Remove the rightmost digit from x by dividing it by 10 (x /= 10). c. Check if adding digit to rev would cause an overflow. If it would, return 0.
  3. Return rev, which now holds the reversed integer.

Explanation of the overflow check:

  • For positive numbers: If rev is greater than Integer.MAX_VALUE / 10, adding any more digits will cause an overflow.
  • For negative numbers: If rev is less than Integer.MIN_VALUE / 10, adding any more digits will cause an overflow.

If the input x is 0, the loop will not run, and 0 will be returned, which is the correct result.

In the provided code, digit > 7 and digit < -8 are used as conditions to check for overflow. Let’s break down why these conditions are used and how they help prevent overflow when reversing the digits of an integer.

  1. For positive numbers:
    • When reversing a positive number, if the reversed number (rev) is greater than Integer.MAX_VALUE / 10, then adding the next digit (digit) will cause an overflow. For example, if rev is 214748364, and the next digit is 8, adding 8 would result in a number greater than Integer.MAX_VALUE.
    • Therefore, the condition rev > Integer.MAX_VALUE / 10 is used to check if adding the next digit will cause an overflow.
    • Additionally, if rev is exactly equal to Integer.MAX_VALUE / 10 (i.e., rev is 214748364), and the next digit (digit) is greater than 7, adding the next digit will also cause an overflow. This is because the maximum positive 32-bit integer is 2147483647, and adding any digit greater than 7 will exceed this limit.
  2. For negative numbers:
    • When reversing a negative number, if the reversed number (rev) is less than Integer.MIN_VALUE / 10, then adding the next digit (digit) will cause an overflow. For example, if rev is -214748364, and the next digit is -8, adding -8 would result in a number less than Integer.MIN_VALUE.
    • Therefore, the condition rev < Integer.MIN_VALUE / 10 is used to check if adding the next digit will cause an overflow.
    • Additionally, if rev is exactly equal to Integer.MIN_VALUE / 10 (i.e., rev is -214748364), and the next digit (digit) is less than -8, adding the next digit will also cause an overflow. This is because the minimum negative 32-bit integer is -2147483648, and adding any digit less than -8 will exceed this limit.

In summary, the conditions digit > 7 and digit < -8 are used to check if adding the next digit will cause an overflow in the positive and negative directions, respectively, when reversing the digits of an integer.

Leave a Reply

Your email address will not be published. Required fields are marked *