Check if a number is palindrome or not without using any extra space | Set 2
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.
Examples:
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Input: n = 2332
Output: Yes it is Palindrome.
Explanation:
original number = 2332
reversed number = 2332
Both are same hence the number is palindrome.Input: n=1111
Output: Yes it is Palindrome.Input: n = 1234
Output: No not Palindrome.
Other Approach: The other recursive approaches and the approach to compare the digits are discussed in Set-1 of this article. Here, we are discussing the other approaches.
Approach: This approach depends upon 3 major steps, find the number of digits in the number. Partition the number into 2 parts from the middle. Take care of the case when the length is odd, in which we will have to use the middle element twice. Check whether the number in both numbers are the same or not. Follow the steps below to solve the problem:
- Initialize the variable K as the length of the number n.
- Initialize the variable ans as 0.
- Iterate over the range [0, K/2) using the variable i and perform the following tasks:
- Put the value of n%10 in the variable ans and divide n by 10.
- If K%2 equals 1 then put the value of n%10 in the variable ans.
- After performing the above steps, if ans equals n then it’s a pallindrome otherwise not.
Below is the implementation of the above approach.
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std; // Function to find if the number is a// pallindrome or notbool isPalindrome(int n){ if (n < 0) return false; if (n < 10) return true; // Find the length of the number int K = ceil(log(n) / log(10)); int ans = 0; // Partition the number into 2 halves for (int i = 0; i < K / 2; i++) { ans = ans * 10 + n % 10; n = n / 10; } if (K % 2 == 1) ans = ans * 10 + n % 10; // Equality Condition return (ans == n);} // Driver Codeint main(){ isPalindrome(1001) ? cout << "Yes, it is Palindrome" : cout << "No, not Palindrome"; return 0;} |
Yes, it is Palindrome
Time Complexity: O(K), where K is the number of digits
Auxiliary Approach: O(1)


