Winsem2023-24 Msts601l TH Ch2023240503480 Reference Material I 05-02-2024 Binary Palindrome

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

BINARY PALINDROME

BINARY PALINDROME

Introduction
You are given an integer ‘X’, you need to convert the integer to binary
format and check if the binary format is palindrome or not

For Example, 5 i.e. 101, 27 i.e. 11011 are numbers whose binary
representations are palindromes. Whereas 10 i.e. 1011 and 20 i.e. 10100 are
not palindromes
BINARY PALINDROME

The problem is very similar to checking whether a string is palindrome or


not. Hence the binary representation must be strings.

We start from leftmost and rightmost bits and compare bits one by one. If
we find a mismatch, then return false.

We can use the regular palindrome program. Converting integer ‘val’ to


binary is as easy as Integer.toBinaryString(val)
BINARY PALINDROME

Find the nth number whose binary representation is a palindrome

A special caution that you should not consider the leading zeros,
while considering the binary representation.

Approach: Traverse through all the integers from 1 to 2^31 – 1 and


increment palindrome count, if the number is a palindrome. When
the palindrome count reaches the required n, break the loop and
return the current integer.
BINARY PALINDROME

Programs

Sample IO
Input : 1
Output : 1
1st Number whose binary representation
is palindrome is 1 (1)

Input : 9
Output : 27
9th Number whose binary representation
is palindrome is 27 (11011)
BINARY PALINDROME

import java.io.*; public static void main(String[] args) {


class Main { Scanner s1=new Scanner(System.in);
public static boolean Palindrome(int N) System.out.println("enter the
{ number-");
String s = Integer.toBinaryString(N); int x=s1.nextInt();
int i = 0, j = s.length() - 1; System.out.println(Palindrome(x));
while (i < j) { }
if (s.charAt(i) != s.charAt(j)) { }
return false;
}
i++;
j--;
}
return true;
}
BINARY PALINDROME

import java.io.*; if (s.equals(s1)) {


class Binary { System.out.println("True");
public static void main(String[] }
args) else
{ System.out.println("False");
int x=9; }
String s = Integer.toBinaryString(x); }
boolean ans = false;
String s1="";
for (int i = s.length() - 1; i
>= 0; i--) {
s1 = s1 + s.charAt(i);
}
BINARY PALINDROME

class GFG
{ public static void main(String[] args) {
static String bin(int n) int x = 9;
{ System.out.println(checkPalindrome(x));
String ans = ""; x = 10;
while(n > 0){ System.out.println(checkPalindrome(x));
ans = (Integer.toString(n&1)) + ans; }
n >>= 1; }
}
return ans;
}
static int checkPalindrome(int n){
String s1 = bin(n);
StringBuilder s2 = new
StringBuilder(s1);
s2 = s2.reverse();
return s1.equals(s2.toString()) ? 1 : 0;
}
BINARY PALINDROME

public class Main {


public static boolean public static void main(String[] args) {
isBinaryPalindrome(int num) { int num = 9;
int revBinary = 0; System.out.println(isBinaryPalindrome(num));
int copyNum = num; num = 10;
while (copyNum != 0) { System.out.println(isBinaryPalindrome(num));
revBinary = (revBinary << 1) }
| (copyNum & 1); }
copyNum >>= 1;
}
return revBinary == num;
}
/ethnuscodemithra Ethnus Codemithra /ethnus /code_mithra

https://learn.codemithra.com

codemithra@ethnus.com +91 71115 095 +91 9019 921 340


095

You might also like