Check if a number is palindrome or not without using any extra space | Set 2 - GeeksforGeeks

20 Dec, 2021
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 = 2332Output: Yes it is Palindrome.Explanation:original number = 2332reversed number = 2332Both are same hence the number is palindrome.
Input: n=1111Output: Yes it is Palindrome.
Input: n = 1234Output: 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.
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++
Java
Python3
C#
Javascript
// 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;}
// Java program for the above approachimport java.util.*; class GFG{ // Function to find if the number is a// pallindrome or notstatic 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 Codepublic 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 approachimport math as Math # Function to find if the number is a# pallindrome or notdef 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 Codeprint("Yes, it is Palindrome") if isPalindrome(    1001) else print("No, not Palindrome") # This code is contributed by Saurabh jaiswal
// C# program for the above approachusing System; class GFG{   // Function to find if the number is a  // pallindrome 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       // pallindrome 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>
  
Yes, it is Palindrome
 
Time Complexity: O(K), where K is the number of digitsAuxiliary Approach: O(1)
 
lokeshpotta20
29AjayKumar
_saurabh_jaiswal
number-digits
palindrome
Divide and Conquer
Mathematical
Mathematical
Divide and Conquer
palindrome
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Binary Search In JavaScript
Find a Fixed Point (Value equal to index) in a given array
Binary Search (bisect) in Python
K-th Element of Two Sorted Arrays
Convex Hull using Divide and Conquer Algorithm
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7