Closest Numeric Palindrome Problem

Closest Numeric Palindrome Problem — ExecCode Hard DSA Practice

Solve the Closest Numeric Palindrome problem on ExecCode. Free online hard DSA practice in String. Write and run code in Java, C++, Python — no signup required to run.

Problem description

Given an integer as a string n, find the closest integer palindrome that is not n itself. Distance is absolute difference. Ties: pick the smaller integer. Leading zeros are not allowed in the answer except for 0. This is a nasty OA digit-mirror problem. Return the palindrome as a string. You are given: n — numeric string without leading zeros unless it is 0 Return numeric string of the nearest palindrome. For single digits the neighbors are n-1 and n+1 (with 0's neighbor 1).

Examples

Input {'n': '123'}; Output 121. Input {'n': '1'}; Output 0. Input {'n': '99'}; Output 101

Constraints

n itself is disallowed even if it is already a palindrome Consider shorter and longer palindromes (999 vs 1001)

Practice Closest Numeric Palindrome free on ExecCode. Browse DSA problems, topic map, and placement guides.