Check if a number is palindrome or not without using any extra space | Set 2
Last Updated :
02 Aug, 2022
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:
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 palindrome otherwise not.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
bool isPalindrome( int n)
{
if (n < 0)
return false ;
if (n < 10)
return true ;
int K = ceil ( log (n) / log (10));
int ans = 0;
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;
return (ans == n);
}
int main()
{
isPalindrome(1001) ? cout << "Yes, it is Palindrome"
: cout << "No, not Palindrome" ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean isPalindrome( int n)
{
if (n < 0 )
return false ;
if (n < 10 )
return true ;
int K = ( int ) Math.ceil(Math.log(n) / Math.log( 10 ));
int ans = 0 ;
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 ;
return (ans == n);
}
public static void main(String[] args)
{
System.out.print(isPalindrome( 1001 ) ? "Yes, it is Palindrome"
: "No, not Palindrome" );
}
}
|
Python3
import math as Math
def isPalindrome(n):
if (n < 0 ):
return False
if (n < 10 ):
return True
K = Math.ceil(Math.log(n) / Math.log( 10 ))
ans = 0
for i in range ( 0 , K / / 2 ):
ans = ans * 10 + n % 10
n = Math.floor(n / 10 )
if (K % 2 = = 1 ):
ans = ans * 10 + n % 10
return (ans = = n)
print ( "Yes, it is Palindrome" ) if isPalindrome(
1001 ) else print ( "No, not Palindrome" )
|
C#
using System;
class GFG
{
static bool isPalindrome( int n)
{
if (n < 0)
return false ;
if (n < 10)
return true ;
int K = ( int )Math.Ceiling(Math.Log(n) / Math.Log(10));
int ans = 0;
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;
return (ans == n);
}
public static void Main()
{
Console.Write(isPalindrome(1001) ? "Yes, it is Palindrome" : "No, not Palindrome" );
}
}
|
Javascript
<script>
function isPalindrome(n) {
if (n < 0)
return false ;
if (n < 10)
return true ;
let K = Math.ceil(Math.log(n) / Math.log(10));
let ans = 0;
for (let i = 0; i < K / 2; i++) {
ans = ans * 10 + n % 10;
n = Math.floor(n / 10);
}
if (K % 2 == 1)
ans = ans * 10 + n % 10;
return (ans == n);
}
isPalindrome(1001) ? document.write( "Yes, it is Palindrome" )
: document.write( "No, not Palindrome" );
</script>
|
Output
Yes, it is Palindrome
Time Complexity: O(K), where K is the number of digits
Auxiliary Approach: O(1)
Share your thoughts in the comments
Please Login to comment...