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 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 above idea :
Time complexity : O(d) ( d is the number of digit in given number )
- Absolute difference between the first X and last X Digits of N
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1
- Check if a number with even number of digits is palindrome or not
- Check if a number is Palindrome
- Check if a number is Palindrome in PL/SQL
- Program to check the number is Palindrome or not
- Given a number, find the next smallest palindrome
- Check if number is palindrome or not in Octal
- Sentence Palindrome (Palindrome after removing spaces, dots, .. etc)
- Bash program to check if the Number is a Palindrome
- Recursive program to check if number is palindrome or not
- Generate number with given operation and check if it is palindrome
- To check a number is palindrome or not without using any extra space
- Count the number of digits of palindrome numbers in an array
- Minimum number of deletions to make a string 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 firstname.lastname@example.org. 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.