We need to check whether an integer reads the same forwards and backwards. The string approach is direct: convert the number to a string and check if the string is a palindrome. That works, but the follow-up asks us to avoid string conversion.
The arithmetic alternative reverses or compares the digits of a number using only math. You can extract digits from the end of a number with modulo and division, and build a reversed number digit by digit. If the reversed number equals the original, the number is a palindrome.
Two cases let us answer immediately without any reversal. Negative numbers are never palindromes, because the minus sign sits at the front and has no counterpart at the back. Numbers ending in 0 are never palindromes either, unless the number is 0 itself, because a palindrome's first digit would then have to be 0, and no integer is written with a leading zero.
-2^31 <= x <= 2^31 - 1 → The input is a single 32-bit integer. There is no array to iterate over, so the relevant cost is the number of digits, which is proportional to log10(x). This rules out anything more expensive than a single pass over the digits.Convert the integer to a string and check if the string reads the same forwards and backwards. Place one pointer at the start and one at the end, compare the characters they point to, and move both inward. If any pair differs, the number is not a palindrome.
This works for negative inputs without a special case: the minus sign at index 0 never matches a digit at the end, so the comparison fails on the first step.
This allocates O(d) extra memory for the string and ignores the follow-up's request to avoid string conversion. The next approach reverses the digits with arithmetic and removes the allocation.
Reverse the entire number with arithmetic instead of converting to a string. The expression x % 10 returns the last digit, and x / 10 removes it. Repeating this reads the digits from right to left. Each new digit is appended to a reversed accumulator with reversed = reversed * 10 + digit, where the multiply by 10 shifts the digits already collected one place left. When the original and reversed values match, every digit equals its mirror, which is the definition of a palindrome.
Negative numbers are handled with one check up front: the minus sign sits at the front and never appears at the back, so they return false before the loop runs.
Because reversing the full number can exceed the 32-bit range, the accumulator uses a 64-bit type in the implementations below.
reversed to 0.x % 10), append it to reversed (reversed * 10 + digit), and remove the last digit from x (x / 10).reversed with the original value. If equal, it's a palindrome.This runs in O(1) space, but it reverses the entire number when comparing half against half would suffice, and it needs a wider integer type to hold the full reversal. The next approach reverses only the second half of the digits, which does half the work and stays within the 32-bit range.
Reverse only the second half of the digits and compare it with the first half. For 1221, the reversed last two digits give 12, and the remaining first half is also 12. They match, so the number is a palindrome.
The stopping point is the second half. Keep extracting digits from the end of x into reversedHalf until reversedHalf is greater than or equal to what remains in x. At that point exactly half the digits have moved across (one past half for odd-length numbers).
This has two advantages over full reversal: it processes half the digits, and reversedHalf never exceeds the original number, so no value can overflow a 32-bit integer.
The loop condition x > reversedHalf decides when half the digits have moved across. Each iteration moves one digit from x to reversedHalf, so x shrinks and reversedHalf grows. The loop stops the moment reversedHalf reaches or passes x, which happens at the midpoint.
For even-length numbers like 1221, after two iterations x = 12 and reversedHalf = 12, and the loop stops because 12 is not greater than 12. Comparing x == reversedHalf checks both halves directly.
For odd-length numbers like 12321, after three iterations x = 12 and reversedHalf = 123. The middle digit (3) sits in reversedHalf, so dividing it out with reversedHalf / 10 gives 12, and x == reversedHalf / 10 compares the two halves while ignoring the middle digit, which matches itself in any palindrome.
reversedHalf to 0.reversedHalf, extract the last digit of x and append it to reversedHalf.x == reversedHalfx == reversedHalf / 10 (the middle digit doesn't need to match anything)