Palindrome numbers | Practice | GeeksforGeeks

Check if the binary representation of a number is palindrome or not.
 
Example 1:
Input:
N = 17
Output:
1
Explanation:
(17)10 = (10001)2
Which is pallindrome hence
output is 1.


Example 2:
Input:
N = 16
Output:
0
Explanation:
(16)10 = (10000)2
Which is not pallindrome 
hence output is 0.


Your Task:
You don't need to read input or print anything. Your task is to complete the function isPallindrome() which takes an integer N as input parameters and returns 1 if the binary equivalent of N is a palindrome.
 
Expected Time Complexity: O(log N)
Expected Space Complexity: O(logN)
 
Constraints:
1 <= N <= 1018
0
amitrrrvaa000317 hours ago
Python :
TC : 0.06/1.13
       a = bin(N)       b = a[2:]       if b == b[::-1]:           return 1       else:           return 0
0
mayank20211 month ago
C++int isPallindrome(long long int N)   {   long long int N1=N;   string s="";   while(N1)    {        s=to_string(N1&1)+s;        N1=N1>>1;    }    int i=0, j=s.length()-1;    while(i<=j)     {         if(s[i] !=s[j]) return 0;         else          i++, j--;     }     return 1;   }
0
taiphanvan24032 months ago
bool check(string x){
        int l=0,r=x.size()-1;
        while(l<r){
            if(x[l]!=x[r]) return false;
            l++;r--;
        }
        return true;
    }
    int isPallindrome(long long int N){
        string str="";
        while(N){
            str=(char)(N%2)+str;
            N/=2;
        }
        return check(str);
    }
0
hanumanmanyam8372 months ago
static long isPallindrome(long N){
        // code here
        String org="";
        String rev="";
        while(N!=0)
        {
            long rem=N%2;
            org+=rem;
            rev=rem+rev;
            N=N/2;
        }
        if(org.equals(rev))
        {
            return 1;
        }
        return 0;
        
    }
0
chechipresh2 months ago

class Solution{
    static long isPallindrome(long N){
        // code here 
        String s="",r="";
        char ch;
        long d;
         
        while(N!=0){
           d=N%2;
           s=d+s;
           N/=2;
        }
        
        for (int i=0;i<s.length();i++){
            ch=s.charAt(i);
            r=ch+r;
        }
        if (r.equals(s)==true)
        return 1;
        return 0;
    }
}
+2
shubhamkhavare2 months ago
Simple Java Solution:
String s = new String();       while(N != 0)       {           long m = N%2;           s += m;           N = N/2;       }       StringBuilder sb = new StringBuilder();       for(int i = s.length()-1 ; i >= 0 ; i--)       {           sb.append(s.charAt(i));       }       if(sb.toString().equals(s))       {           return 1;       }       return 0;
0
raghavendra7773 months ago
Python 0.1 S
def isPallindrome(self, N):       k=bin(N)       #print(k)       k=k[2:]       if k==k[::-1]:           return 1       else :           return 0
0
ankitparashxr4 months ago
time-0.3 sec
class Solution{   static long isPallindrome(long n)   {       String bin = "";       long temp = n;       while(temp!=0)       {           long rem = temp%2;           bin += rem+"";           temp = temp/2;       }       int len = bin.length();       if(bin.charAt(0) != bin.charAt(len-1))       {           return 0;       }       for(int i =0,j = bin.length()-1;i<=len/2;i++,j--)       {           if(bin.charAt(i)!=bin.charAt(j))           {               return 0;           }       }       return 1;   }}
+2
shubham1912994 months ago
C++ Solution 
int isPallindrome(long long int n){
        // code here
        
        string s="";
        if((n & 1)==0) return 0; // If Even number return 0;
        else {
            while(n){
                s+=(n&1);
                n>>=1;
            }
        }
        int len = s.size();
        for(int i=0;i<len/2;i++){
            if(s[i]!=s[len-i-1]) return 0;
        }
        return 1;
    }
0
krishnagautam58994 months ago
// Failled the test case   N=91  
int Binary(int n){       if(n==0){  return 0;  }       else{           return n%2 +10*Binary(n/2);       }   }     int RevNum(int n){       if(n==0){  return 0;   }       else{           return n%10+10*RevNum(n/10) ;       }   }     int isPallindrome(long long int N){       // code here       long long int n1=Binary(N);       long long int n2=RevNum(n1);                if(n1==n2){  return 1;  }        else{  return 0;        }   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.