Thursday, December 26, 2024
Google search engine
HomeData Modelling & AICheck if a number is palindrome or not without using any extra...

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:

  • 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++




// 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




// 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


Python3




# 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#




// 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


Javascript




<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)

 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments