Math · Easy
Palindrome Number Check
Solve it on DeVert Campus - free code runner
Problem
Given an integer n, determine whether it is a palindrome, meaning it reads the same forwards and backwards in decimal. Negative numbers are never palindromes because of the leading minus sign. Do not convert the number to a string - reverse it arithmetically using digit extraction instead.
Input and output
Input: A single line containing one integer n.
Output: Print "YES" if n is a palindrome, otherwise print "NO".
Example
Input: 12321 Output: YES
Constraints
-2000000000 <= n <= 2000000000
Key observation
Using a 64-bit type for the reversed value (even though n itself might fit in 32 bits) avoids overflow during the reversal process, since a large n's reversed form could otherwise exceed a 32-bit integer's range mid-computation.
Intuition
Check whether a number reads the same forwards and backwards, without converting it to a string.
Brute force: Convert the number to a string and check if it reads the same forwards and backwards using two pointers - this works and is simple, but the problem specifically asks for an ARITHMETIC approach (digit extraction), avoiding any string conversion.
Optimised: Extracting digits one at a time via modulo and division, and building up a reversed value the same way you'd reverse a linked list or array, achieves the reversal purely through arithmetic - directly comparing the fully reversed value to the original then answers the palindrome check.
Step by step
1. If n is negative, immediately answer NO (the leading minus sign breaks any possible symmetry).
2. Build the REVERSED number digit by digit using n % 10 (extract the last digit) and n / 10 (remove the last digit), exactly like reversing an integer arithmetically.
3. Compare the fully reversed value to the original n.
Complexity
Time: O(log n) - proportional to the number of digits in n.
Space: O(1) - just a few integer variables.
Edge cases
Negative numbers always answer NO. n=0 is a palindrome (YES). Numbers ending in 0 other than 0 itself (like 1200) can never be palindromes, since a reversed number cannot have leading zeros.
Interview tip
Be ready to justify why negative numbers are automatically excluded - the leading minus sign has no matching counterpart at the end of the number, so no negative number can ever be symmetric.