Hard

Quiz

#564 Find the Closest Palindrome

APPROACH

Given a string n that encodes a positive integer, find the nearest palindrome — an integer other than n itself that reads the same forwards and backwards — minimizing the absolute difference. Break ties by returning the smaller value.

Example 1:

Input: n = "123"
Output: "121"

Example 2:

Input: n = "1"
Output: "0"
Explanation: 0 and 2 are the closest palindromes but we return the smallest which is 0.
1 of 4
1:00

What is the optimal approach for this problem?