Eg :

4 --To Binary--> 100 : Not palindrome

5 --To Binary--> 101 : palindrome

simple approach:

1. reverse the bits of given number. O(log(n))

2. compare both the numbers.

3. if same then palindrome else not. source code:

4 --To Binary--> 100 : Not palindrome

5 --To Binary--> 101 : palindrome

simple approach:

1. reverse the bits of given number. O(log(n))

2. compare both the numbers.

3. if same then palindrome else not. source code:

#include <iostream> using namespace std; int reverse(int n) { int i = 0, j = n; while(j) i = (i << 1) | (j & 1); j >>= 1; } return(i); } int main() { int rev , n=5; rev = reverse(n); rev==n?cout<<"palindrome":cout<<"not palindrome"; return 0; }

## 0 comments:

## Post a Comment