Reverse Integer Problem and Solution

1. Introduction

The "Reverse Integer" problem involves reversing an integer. The challenge is to handle potential overflow and ensure the reversed integer remains within the bounds of a 32-bit signed integer.

2. Solution in C++

#include <iostream>
#include <climits>
using namespace std;

int reverse(int x) {
    long reversed = 0;
    while (x != 0) {
        reversed = reversed * 10 + x % 10;
        x /= 10;
        if (reversed < INT_MIN || reversed > INT_MAX) {
            return 0;
        }
    }
    return reversed;
}

// Example usage
int main() {
    int num = 123;
    cout << "Reversed: " << reverse(num) << endl;
    return 0;
}

Output:

Reversed: 321

Explanation:

1. The function reverse reverses an integer x.

2. It uses a long variable to handle potential overflow.

3. The integer is reversed by multiplying the current reversed number by 10 and adding the last digit of x.

4. If the reversed number goes out of the 32-bit integer range, 0 is returned.

5. The function returns the reversed number if no overflow occurs.

3. Solution in Java

public class ReverseInteger {
    public static int reverse(int x) {
        long reversed = 0;
        while (x != 0) {
            reversed = reversed * 10 + x % 10;
            x /= 10;
            if (reversed < Integer.MIN_VALUE || reversed > Integer.MAX_VALUE) {
                return 0;
            }
        }
        return (int) reversed;
    }

    // Example usage
    public static void main(String[] args) {
        int num = 123;
        System.out.println("Reversed: " + reverse(num));
    }
}

Output:

Reversed: 321

Explanation:

1. Similar to the C++ solution, the reverse method in Java uses a long to prevent overflow.

2. It reverses the number using the same logic: multiplying the current reversed value by 10 and adding the last digit.

3. It checks for overflow against the bounds of an int.

4. The method returns the reversed number cast to an int if it's within the valid range.

4. Solution in Python

def reverse(x):
    INT_MAX, INT_MIN = 2**31 - 1, -2**31
    reversed = 0
    while x != 0:
        digit = int(x % 10)
        x = int(x / 10)
        if reversed > INT_MAX//10 or (reversed == INT_MAX//10 and digit > INT_MAX%10):
            return 0
        if reversed < INT_MIN//10 or (reversed == INT_MIN//10 and digit < INT_MIN%10):
            return 0
        reversed = reversed * 10 + digit
    return reversed

# Example usage
num = 123
print("Reversed:", reverse(num))

Output:

Reversed: 321

Explanation:

1. The reverse function in Python also handles overflow.

2. It defines the maximum and minimum 32-bit integer limits.

3. The function checks for overflow conditions before adding the next digit to the reversed number.

4. The number is reversed using similar logic as in previous solutions.

5. The function returns 0 if an overflow is detected, otherwise the reversed number.

5. Solution in C

#include <stdio.h>
#include <limits.h>

int reverse(int x) {
    long reversed = 0;
    while (x != 0) {
        reversed = reversed * 10 + x % 10;
        x /= 10;
        if (reversed < INT_MIN || reversed > INT_MAX) {
            return 0;
        }
    }
    return reversed;
}

// Example usage
int main() {
    int num = 123;
    printf("Reversed: %d\n", reverse(num));
    return 0;
}

Output:

Reversed: 321

Explanation:

1. The C solution is similar to the C++ and Java solutions.

2. It uses a long to avoid overflow and reverses the number by the same method.

3. The function checks if the reversed number is within the 32-bit integer range.

4. It returns 0 if overflow occurs, otherwise the reversed number.


Comments