Given a number ‘n’ and our goal is to find out it is palindrome or not without using
any extra space. We can’t make a new copy of number .
Input : 2332 Output : Yes it is Palindrome. Explanation: original number = 2332 reversed number = 2332 Both are same hence the number is palindrome. Input :1111 Output :Yes it is Palindrome. Input : 1234 Output : No not Palindrome.
A recursive solution is discussed in below post.
Check if a number is Palindrome
In this post a different solution is discussed.
1) We can compare the first digit and the last digit, then we repeat the process.
2) For the first digit, we need the order of the number. Say, 12321. Dividing this by 10000 would get us the leading 1. The trailing 1 can be retrieved by taking the mod with 10.
3 ) Now, to reduce this to 232.
(12321 % 10000)/10 = (2321)/10 = 232
4 ) And now, the 10000 would need to be reduced by a factor of 100.
Here is the implementation of the above algorithm :
Yes, it is Palindrome
This article is contributed by Abhijit Shankhdhar. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Check if the characters in a string form a Palindrome in O(1) extra space
- Length of longest palindrome list in a linked list using O(1) extra space
- Sum of all substrings of a string representing a number | Set 2 (Constant Extra Space)
- Rearrange an array so that arr[i] becomes arr[arr[i]] with O(1) extra space
- Check if a number is Palindrome
- Check if a number is Palindrome in PL/SQL
- Merge two sorted arrays with O(1) extra space
- Program to check the number is Palindrome or not
- Check if number is palindrome or not in Octal
- Efficiently merging two sorted arrays with O(1) extra space
- Generate number with given operation and check if it is palindrome
- Check if binary representation of a number is palindrome
- Recursive program to check if number is palindrome or not
- Bash program to check if the Number is a Palindrome
- Check whether the given floating point number is a palindrome
Improved By : jit_t