Java Number Program · OT22
Check If a Number Is a Palindrome in Java
Reverse only the second half of the digits and compare it with the first half. Half a number can never overflow, and no string is needed.
Problem Statement
Return true if a non-negative integer reads the same from left to right and from right to left, without converting it to a string.
Negative numbers are not palindromes because of the minus sign, and a number ending in 0 (other than 0 itself) cannot be one either.
Input/Output Examples
40904OutputtrueExplanation: 4 0 9 0 4 mirrors around the 9.1230OutputfalseExplanation: it would have to start with 0.-121OutputfalseExplanation: "-121" reversed is "121-".Visual Explanation
- n = 40904half0move 4 to the half n = 4090, half = 4
- n = 4090half4move 0 n = 409, half = 40
- n = 409half40move 9 n = 40, half = 409
- Comparevalues40409 / 10 = 40n ≤ half: stop
- Donetrue
n to half until half is at least as large. For an odd number of digits, half / 10 drops the middle digit.The loop stops at the middle of the number, so it does half the work of a full reversal, and the reversed half always fits in an int.
Approaches
Both approaches reverse digits arithmetically. One stops halfway; the other reverses everything in a long.
Approach 1: Reverse Half of the Digits (Recommended)
Idea: move digits from the end of n to reversedHalf until it is at least as large as what remains.
- Return
falsefor negatives and for non-zero numbers ending in 0. - While
n > reversedHalf, move the last digit ofnintoreversedHalf. - Compare
nwithreversedHalf, or withreversedHalf / 10for an odd digit count.
Time: O(d) · Space: O(1). d is the number of digits.
Approach 2: Full Reverse in a long
Idea: reverse every digit into a long and compare with the original.
- Return
falsefor negatives. - Build the reversed value digit by digit in a
long. - Compare it with
n.
Time: O(d) · Space: O(1).
Java Solutions
Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.
Approach 1: Reverse Half of the Digits (Recommended)
The trailing-zero check is needed because the half-reversal would otherwise accept numbers like 10.
public class NumPalHalf {
// Reverse only the second half of the digits and compare it with the first half
static boolean isPalindrome(int n) {
if (n < 0 || (n % 10 == 0 && n != 0)) {
return false; // a sign or a trailing zero can never match
}
int reversedHalf = 0;
while (n > reversedHalf) {
reversedHalf = reversedHalf * 10 + n % 10;
n /= 10;
}
return n == reversedHalf || n == reversedHalf / 10; // the /10 drops the middle digit
}
public static void main(String[] args) {
int[] inputs = {40904, 1221, 1230, -121, 7};
for (int n : inputs) {
System.out.println(n + " -> " + isPalindrome(n));
}
}
}
40904 -> true 1221 -> true 1230 -> false -121 -> false 7 -> true
Time complexity: O(d) · Space complexity: O(1).
Approach 2: Full Reverse in a long
Simpler to explain; the long prevents overflow when reversing numbers such as 2147483647.
public class NumPalFull {
// Reverse all digits in a long (so it cannot overflow) and compare
static boolean isPalindrome(int n) {
if (n < 0) {
return false;
}
long reversed = 0;
int x = n;
while (x > 0) {
reversed = reversed * 10 + x % 10;
x /= 10;
}
return reversed == n;
}
public static void main(String[] args) {
int[] inputs = {40904, 1221, 1230, -121, 7};
for (int n : inputs) {
System.out.println(n + " -> " + isPalindrome(n));
}
}
}
40904 -> true 1221 -> true 1230 -> false -121 -> false 7 -> true
Time complexity: O(d) · Space complexity: O(1).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Reverse Half of the Digits | O(d) | O(1) |
| Full Reverse in a long | O(d) | O(1) |
Here d is the number of digits, at most 10 for an int. The half method does about d / 2 steps.
Interview Tips and Recommended Approach
- Mention overflow. Reversing 1999999999 in an
intoverflows; reversing half cannot. - Handle trailing zeros before the loop.
- Explain the odd-length case and why
/ 10removes the middle digit. - Know the string version, but say why the arithmetic one is preferred: no allocation.
Recommended Approach: Reverse Half of the Digits
Why: it needs no string, cannot overflow and stops halfway, which makes it the most efficient arithmetic answer.