Closest Palindrome Problem Statement

You are given a string 'S' that represents a number. Your task is to find the closest palindromic number to this integer represented by 'S'. The closest number is defined as the one for which the absolute difference between the given integer represented by 'S' and the palindrome number is minimum. If multiple numbers have the same difference, return the smaller integer.

Example:

Input:
S = "121"
Output:
111
Explanation:

For the input 'S' = 121, the nearest palindromic numbers are 111 and 131. Both have the same absolute difference from 121, but 111 is smaller, so the answer is 111.

Constraints:

  • 1 <= T <= 50
  • 0 <= No of digits in 'S' <= 18
  • Time Limit: 1 sec
AnswerBot
4d

Find the closest palindromic number to a given integer represented by a string.

  • Convert the string to an integer and iterate to find the closest palindromic number.

  • Check for palindromic numbers by reve...read more

Help your peers!
Add answer anonymously...
Microsoft Corporation SDE-2 Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter