Open In App

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:



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:

Below is the implementation of the above approach.




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find if the number is a
// palindrome or not
bool 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 Code
int main()
{
    isPalindrome(1001) ? cout << "Yes, it is Palindrome"
                       : cout << "No, not Palindrome";
    return 0;
}




// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find if the number is a
// palindrome or not
static boolean isPalindrome(int n)
{
    if (n < 0)
        return false;
    if (n < 10)
        return true;
 
    // Find the length of the number
    int K = (int) Math.ceil(Math.log(n) / Math.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 Code
public static void main(String[] args)
{
    System.out.print(isPalindrome(1001) ? "Yes, it is Palindrome"
                       : "No, not Palindrome");
}
}
 
// This code is contributed by 29AjayKumar




# Python code for the above approach
import math as Math
 
# Function to find if the number is a
# palindrome or not
def isPalindrome(n):
    if (n < 0):
        return False
    if (n < 10):
        return True
 
    # Find the length of the number
    K = Math.ceil(Math.log(n) / Math.log(10))
 
    ans = 0
 
    # Partition the number into 2 halves
    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
 
    # Equality Condition
    return (ans == n)
 
# Driver Code
print("Yes, it is Palindrome") if isPalindrome(
    1001) else print("No, not Palindrome")
 
# This code is contributed by Saurabh jaiswal




// C# program for the above approach
using System;
 
class GFG
{
 
  // Function to find if the number is a
  // palindrome or not
  static bool isPalindrome(int n)
  {
    if (n < 0)
      return false;
    if (n < 10)
      return true;
 
    // Find the length of the number
    int K = (int)Math.Ceiling(Math.Log(n) / Math.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 Code
  public static void Main()
  {
    Console.Write(isPalindrome(1001) ? "Yes, it is Palindrome" : "No, not Palindrome");
  }
}
 
// This code is contributed by Saurabh Jaiswal




<script>
       // JavaScript code for the above approach
 
       // Function to find if the number is a
       // palindrome or not
       function isPalindrome(n) {
           if (n < 0)
               return false;
           if (n < 10)
               return true;
 
           // Find the length of the number
           let K = Math.ceil(Math.log(n) / Math.log(10));
 
           let ans = 0;
 
           // Partition the number into 2 halves
           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;
 
           // Equality Condition
           return (ans == n);
       }
 
       // Driver Code
       isPalindrome(1001) ? document.write("Yes, it is Palindrome")
           : document.write("No, not Palindrome");
 
 // This code is contributed by Potta Lokesh
   </script>

 
 

Output
Yes, it is Palindrome

 

Time Complexity: O(K), where K is the number of digits
Auxiliary Approach: O(1)

 


Article Tags :