Given a number N. our task is to find the closest Palindrome number whose absolute difference with given number is minimum and absolute difference must be greater than 0.
Input : N = 121 Output : 131 or 111 Both having equal absolute difference with the given number. Input : N = 1234 Output : 1221
Asked In : Amazon
Simple Solution is to find the largest palindrome number which is smaller to given number and also find the first palindrome number which is greater than Given number.we can find there Palindromic numbers by simply decreasing and increasing by one in given number until we find these palindromic numbers.
Below is the implementation of above idea :
An efficient solution is to consider following cases.
Case 1: If a number contains all 9’s then we get next closest Palindrome by simply adding 2 in it. num = 999 : output : num + 2 = 1001.
Case 2 a :One possible way to getting closest palindromic by Copy first half and add mirror image at the end if it. Left half : For example, left side of “123 456” is “123” and left half of “12345” is “1 2”. To convert to palindrome, we can either take the mirror of its left half or take mirror of its right half. However, if we take the mirror of the right half, then the palindrome so formed is not guaranteed to be the closest palindrome. So, we must take the mirror of left side and copy it to right side.
Let's number : 123456 After copy and append reverse of it at the end number looks like: we get palindrome 123321
case 2 b and 2c: Two more possible ways of getting the closest palindromic number by decrementing and incrementing middle digit by one on palindrome.
Below is the implementation of the above idea :
Time complexity: O(d) ( d is the number of digit in given number )
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Sentence Palindrome (Palindrome after removing spaces, dots, .. etc)
- Count all palindrome which is square of a palindrome
- Minimum number closest to N made up of odd digits only
- Find the greater number closest to N having at most one non-zero digit
- Smaller palindromic number closest to N
- Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
- Print a closest string that does not contain adjacent duplicates
- Queries to find the future closest date
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1
- Absolute difference between the first X and last X Digits of N
- Maximum time such that absolute difference between hour and minute lies in given range
- Count of numbers upto N having absolute difference of at most K between any two adjacent digits
- Count of N-digit numbers with absolute difference of adjacent digits not exceeding K
- Count of N-digit numbers with absolute difference of adjacent digits not exceeding K | Set 2
- Generate all N digit numbers having absolute difference as K between adjacent digits
- Number formed by adding product of its max and min digit K times
- Min steps to convert N-digit prime number into another by replacing a digit in each step
- Min number of operations to reduce N to 0 by subtracting any digits from N
- Check if a number with even number of digits is palindrome or not
- Smallest number to be subtracted to convert given number to a palindrome
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.