Given an integer x, determine whether its binary representation is a palindrome. Return true if it is a palindrome; otherwise, return false.
Input:
x = 9
Output:true
Explanation: The binary representation of9is1001, which is a palindrome.
Input:x = 10
Output:false
Explanation: The binary representation of10is1010, which is not a palindrome.
Compare Bits from Both Ends
Compare bits from the leftmost and rightmost positions. If all corresponding bits match, the binary representation is a palindrome.
- Find the number of bits in x.
- Set l = 0 (leftmost) and r = n - 1 (rightmost).
- While l < r, compare bits at positions l and r.
- If they differ, return false; otherwise move inward.
- If no mismatch is found, return true.
#include <iostream>
#include <cmath>
using namespace std;
bool isBinaryPalindrome(int x) {
if (x == 0) return true;
// Find number of bits
int n = log2(x) + 1;
int l = 0, r = n - 1;
while (l < r) {
// Compare bits at positions l and r
if (((x >> l) & 1) != ((x >> r) & 1)) {
return false;
}
l++;
r--;
}
return true;
}
int main() {
cout << (isBinaryPalindrome(9) ? "true" : "false") << endl;
cout << (isBinaryPalindrome(10) ? "true" : "false") << endl;
return 0;
}
class GfG {
static boolean isBinaryPalindrome(int x) {
if (x == 0) return true;
// Find number of bits
int n = (int)(Math.log(x) / Math.log(2)) + 1;
int l = 0, r = n - 1;
while (l < r) {
// Compare bits at positions l and r
if (((x >> l) & 1) != ((x >> r) & 1)) {
return false;
}
l++;
r--;
}
return true;
}
public static void main(String[] args) {
System.out.println(isBinaryPalindrome(9) ? "true" : "false");
System.out.println(isBinaryPalindrome(10) ? "true" : "false");
}
}
import math
def isBinaryPalindrome(x):
if (x == 0) return true;
# Find number of bits
n = int(math.log2(x)) + 1
l, r = 0, n - 1
while l < r:
# Compare bits at positions l and r
if ((x >> l) & 1) != ((x >> r) & 1):
return False
l += 1
r -= 1
return True
if __name__ == "__main__":
print("true" if isBinaryPalindrome(9) else "false")
print("true" if isBinaryPalindrome(10) else "false")
using System;
class GfG {
public static bool IsBinaryPalindrome(int x) {
if (x == 0) return true;
// Find number of bits
int n = (int)(Math.Log(x) / Math.Log(2)) + 1;
int l = 0, r = n - 1;
while (l < r) {
// Compare bits at positions l and r
if (((x >> l) & 1) != ((x >> r) & 1)) {
return false;
}
l++;
r--;
}
return true;
}
public static void Main() {
Console.WriteLine(IsBinaryPalindrome(9) ? "true" : "false");
Console.WriteLine(IsBinaryPalindrome(10) ? "true" : "false");
}
}
function isBinaryPalindrome(x) {
if (x == 0) return true;
// Find number of bits
let n = Math.floor(Math.log2(x)) + 1;
let l = 0, r = n - 1;
while (l < r) {
// Compare bits at positions l and r
if (((x >> l) & 1) !== ((x >> r) & 1)) {
return false;
}
l++;
r--;
}
return true;
}
// Driver Code
console.log(isBinaryPalindrome(9) ? "true" : "false");
console.log(isBinaryPalindrome(10) ? "true" : "false");
Output
true false
Time Complexity: O(log x), as the loop runs once for each bit in x.
Space Complexity: O(1).
Using reverse() Function
Convert the integer to its binary string form and check if it is equal to its reverse using the reverse() function.
- Convert x to a binary string (without leading zeros).
- Store the binary string.
- Reverse it using reverse().
- Compare original and reversed strings.
- If equal, return true; otherwise, return false.
#include <iostream>
#include <bitset>
#include <algorithm>
using namespace std;
bool isBinaryPalindrome(int x) {
// Convert to binary
string binary = bitset<32>(x).to_string();
// Remove leading zeros
binary = binary.substr(binary.find('1'));
string revBinary = binary;
// Reverse the string
reverse(revBinary.begin(), revBinary.end());
return binary == revBinary;
}
int main() {
cout << (isBinaryPalindrome(9) ? "true" : "false") << endl;
cout << (isBinaryPalindrome(10) ? "true" : "false") << endl;
return 0;
}
import java.util.*;
class GfG {
static boolean isBinaryPalindrome(int x) {
// Convert to binary
String binary = Integer.toBinaryString(x);
// Reverse the string
String revBinary = new StringBuilder(binary).reverse().toString();
return binary.equals(revBinary);
}
public static void main(String[] args) {
System.out.println(isBinaryPalindrome(9) ? "true" : "false");
System.out.println(isBinaryPalindrome(10) ? "true" : "false");
}
}
def isBinaryPalindrome(x):
# Convert to binary and remove leading zeros
binary = bin(x)[2:]
# Reverse and compare
return binary == binary[::-1]
if __name__ == "__main__":
print("true" if isBinaryPalindrome(9) else "false")
print("true" if isBinaryPalindrome(10) else "false")
using System;
class GfG {
public static bool IsBinaryPalindrome(int x) {
// Convert to binary
string binary = Convert.ToString(x, 2);
char[] revBinary = binary.ToCharArray();
// Reverse the string
Array.Reverse(revBinary);
return binary == new string(revBinary);
}
public static void Main() {
Console.WriteLine(IsBinaryPalindrome(9) ? "true" : "false");
Console.WriteLine(IsBinaryPalindrome(10) ? "true" : "false");
}
}
function isBinaryPalindrome(x) {
// Convert to binary
let binary = x.toString(2);
// Reverse the string
let revBinary = binary.split("").reverse().join("");
return binary === revBinary;
}
// Driver Code
console.log(isBinaryPalindrome(9) ? "true" : "false");
console.log(isBinaryPalindrome(10) ? "true" : "false");
Output
true false
Time Complexity: O(log x), as operations are performed on all bits of x.
Space Complexity: O(log x), due to storing the binary string and its reverse.
Using Bitwise Reverse
Reverse the binary representation using bitwise operations and compare it with the original number.
- Create a copy of the input and initialize rev = 0.
- Extract bits one by one and build the reversed number using OR.
- Compare the reversed number with the original.
- If equal, return true; otherwise, return false.
#include <iostream>
using namespace std;
bool isBinaryPalindrome(int x) {
int rev = 0, temp = x;
while (temp > 0) {
// Append LSB to rev
rev = (rev << 1) | (temp & 1);
// Shift temp right
temp >>= 1;
}
return x == rev;
}
int main() {
cout << (isBinaryPalindrome(9) ? "true" : "false") << endl;
cout << (isBinaryPalindrome(10) ? "true" : "false") << endl;
return 0;
}
class GfG {
static boolean isBinaryPalindrome(int x) {
int rev = 0, temp = x;
while (temp > 0) {
// Append LSB to rev
rev = (rev << 1) | (temp & 1);
// Shift temp right
temp >>= 1;
}
return x == rev;
}
public static void main(String[] args) {
System.out.println(isBinaryPalindrome(9) ? "true" : "false");
System.out.println(isBinaryPalindrome(10) ? "true" : "false");
}
}
def isBinaryPalindrome(x):
rev, temp = 0, x
while temp > 0:
# Append LSB to rev
rev = (rev << 1) | (temp & 1)
# Shift temp right
temp >>= 1
return x == rev
if __name__ == "__main__":
print("true" if isBinaryPalindrome(9) else "false")
print("true" if isBinaryPalindrome(10) else "false")
using System;
class GfG {
public static bool IsBinaryPalindrome(int x) {
int rev = 0, temp = x;
while (temp > 0) {
// Append LSB to rev
rev = (rev << 1) | (temp & 1);
// Shift temp right
temp >>= 1;
}
return x == rev;
}
public static void Main() {
Console.WriteLine(IsBinaryPalindrome(9) ? "true" : "false");
Console.WriteLine(IsBinaryPalindrome(10) ? "true" : "false");
}
}
function isBinaryPalindrome(x) {
let rev = 0, temp = x;
while (temp > 0) {
// Append LSB to rev
rev = (rev << 1) | (temp & 1);
// Shift temp right
temp >>= 1;
}
return x === rev;
}
// Driver Code
console.log(isBinaryPalindrome(9) ? "true" : "false");
console.log(isBinaryPalindrome(10) ? "true" : "false");
Output
true false
Time Complexity: O(log x)
Space Complexity: O(1)