Home » Technical Interview Questions » Palindrome Number

Palindrome Number


Reading Time - 4 mins


Difficulty Level Easy

Problem Statement

the problem “Palindrome Number” states that you are given an integer number. Check if it is a palindrome or not. Solve this problem without converting the given number into a string.

Palindrome Number

Example

12321
true

Explanation

12321 is a palindrome number because when we reverse 12321 it gives 12321 which is same as the given number. So 12321 is a palindrome number.

-12321
false

Explanation

-12321 is not a palindrome number because when we reverse -12321 it gives 12321- which is not the same as the given number. So -12321 is not a palindrome number.

Approach for Palindrome Number checking

The first approach that comes in our mind is to convert the given number into a string and check if it is palindrome or not. But we can not do this because this approach is restricted and also we require extra space for the string.

We can also observe one important point that a negative number is never a palindrome number.

So we will try to approach it in a different way. We will reverse the number and store it in a variable and compare if it is equal to the original number. If the reversed number is equal to the original number then the number is a palindrome else it is not a palindrome.

To reverse the given number we will perform( remainder= n%10 ) this gives the last digit of the number. We will generate the reversed number by (reversed=reversed*10+remainder). Now we will divide the number by 10 to get the second last digit. We repeat this process until the value of n is greater than zero.

READ  Heap Sort

At last, we will compare if the original number is equal to the reversed number. If yes then the number is a palindrome number else it is not a palindrome number.

Code

C++ code for Palindrome Number

#include<bits/stdc++.h>
using namespace std;
bool isPalindrome(int num){
  if(num < 0) return  false; 
  int reversed = 0, remainder, original = num;
  while(num != 0) {
        remainder = num % 10; 
        reversed = reversed * 10 + remainder; 
        num  /= 10; 
   }
   return original == reversed;
}
  
int main() 
{ 
   if(isPalindrome(12321)) 
     cout<<"Yes, it is Palindrome"; 
   else
     cout<<"No, not Palindrome"; 
}
Yes, it is Palindrome

Java code for Palindrome Number

public class check
{	 
  static  boolean isPalindrome(int num){
   if(num < 0) return  false; 
   int reversed = 0, remainder, original = num;
   while(num != 0) {
        remainder = num % 10; 
        reversed = reversed * 10 + remainder; 
        num  /= 10; 
    }
    return original == reversed;
}
  
public static void main(String args[]){ 
    if(isPalindrome(12321)) 
      System.out.println("Yes, it is Palindrome"); 
    else
      System.out.println("No, not Palindrome"); 
  } 
} 

Yes, it is Palindrome

Complexity Analysis

Time complexity

The time complexity to check if a number is a palindrome or not is  

Space complexity

O(1) because we are using one extra variable to store the reversed number.

Array Interview Questions
Graph Interview Questions
LinkedList Interview Questions
String Interview Questions
Tree Interview Questions