Find the Closest Palindrome
Examples
Example 1:
Input: n = "123"
Output: "121"
Explanation:
Example 2:
Input: n = "1"
Output: "0"
Explanation:
Example 1:
Input: n = "123"
Output: "121"
Explanation:
Example 2:
Input: n = "1"
Output: "0"
Explanation:
Given a string n representing an integer, return the closest integer (not including itself), which is a palindrome. If there is a tie, return the smaller one.
The closest is defined as the absolute difference minimized between two integers.
Interviewer-asked variant (per the Roblox interview report): the candidate was asked to solve it without converting the input to a string or array — i.e., handle the integer in-place. The reference solution below uses string mirroring for clarity; the no-string variant performs the same digit-mirroring with arithmetic on the integer.
Both 121 and 131 differ from 123 by 2; the smaller wins.
0 and 2 are both 1 away from 1; return the smaller (0).
1 <= n.length <= 18n consists of digits only.n does not have leading zeros.n is representing an integer in the range [1, 10^18 - 1].