0% found this document useful (0 votes)
49 views23 pages

Fallsem2024-25 Sts4021 Ss Ch2024250100090 Reference Material I 13-08-2024 Binary Palindrome 14

Uploaded by

dsbaaspirant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views23 pages

Fallsem2024-25 Sts4021 Ss Ch2024250100090 Reference Material I 13-08-2024 Binary Palindrome 14

Uploaded by

dsbaaspirant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

BINARY

PALINDROME
TOPICS

⮚ Introduction

⮚ Explanation

⮚ Coding

⮚ Interview Questions
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
INTRODUCTION

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

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.
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)
Program 1
import java.io.*;
class Main {
public static boolean Palindrome(int N)
{
String s = Integer.toBinaryString(N);
int i = 0, j = s.length() - 1;
while (i < j) {
if (s.charAt(i) != s.charAt(j)) {
return false;
}
i++;
j--;
}
return true;
}
public static void main(String[] args) {
Scanner s1=new Scanner(System.in);
System.out.println("enter the number-");
int x=s1.nextInt();
System.out.println(Palindrome(x));
}}
1. The `Palindrome` method takes an integer `N` as input.
2. It converts the integer `N` to its binary representation using `Integer.toBinaryString(N)` and
stores it in the string `s`.
3. It initializes two pointers `i` and `j` to the start and end of the string `s`, respectively.
4. It enters a loop that continues as long as `i` is less than `j`.
5. Inside the loop, it compares the characters at indices `i` and `j` in the string `s`.
6. If any pair of characters does not match, it returns `false`, indicating that the binary
representation is not a palindrome.
7. If all characters match, it increments `i` and decrements `j` to move the pointers towards the
middle of the string.
8. If the loop completes without returning `false`, it means the binary representation is a
palindrome, and the method returns `true`.
9. In the `main` method, it prompts the user to enter a number using `Scanner`.
10. It calls the `Palindrome` method with the user-entered number `x` and prints the result
(either `true` or `false`) to indicate whether the binary representation of `x` is a palindrome or
Program 2

import java.io.*;
class Binary {
public static void main(String[] args)
{
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);
}
if (s.equals(s1)) {
System.out.println("True");
}
else
System.out.println("False");
}
}
1. Binary Representation: The program converts the decimal number `9` to its
binary representation using `Integer.toBinaryString(x)` and stores it in the string
`s`.

2. Reversing a String: The program then reverses the binary string `s` by iterating
through it in reverse order and appending each character to a new string `s1`.

3. Palindrome Check: The program checks if the original binary string `s` is equal
to the reversed string `s1` using the `equals` method. If they are equal, it means
the binary representation is a palindrome, and the program prints `"True"`.
Otherwise, it prints `"False"`.
Program 3

class BinaryPalindromeChecker {
static String bin(int n) {
String ans = "";
while (n > 0) {
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;
}
public static void main(String[] args) {
int x = 9;
System.out.println(checkPalindrome(x));
x = 10;
System.out.println(checkPalindrome(x));
}
}
class PalindromeChecker {
public static boolean isBinaryPalindrome(int num) {
int revBinary = 0;
int copyNum = num;
while (copyNum != 0) {
revBinary = (revBinary << 1) | (copyNum & 1);
copyNum >>= 1;
}
return revBinary == num;
}
public static void main(String[] args) {
int num = 9;
System.out.println(isBinaryPalindrome(num));
num = 10;
System.out.println(isBinaryPalindrome(num));
}
}
1. Binary Conversion: The "bin" method in "BinaryPalindromeChecker" converts a
decimal number to its binary representation by repeatedly dividing the number by 2
and storing the remainders in reverse order.

2. Palindrome Check: The "checkPalindrome" method in "BinaryPalindromeChecker"


checks if the binary representation of a number is a palindrome by comparing the
original binary string with its reversed version using a "StringBuilder".

3. Bit Manipulation: The "isBinaryPalindrome" method in "PalindromeChecker" reverses


the binary representation of a number using bitwise operations to check if it remains
the same as the original number, indicating a binary palindrome.

4. Shifting and Masking: In "isBinaryPalindrome", the algorithm shifts the reversed


binary number left by 1 bit and then uses bitwise OR with the least significant bit of the
original number to reverse it.

5. Comparison and Return: The methods return "true" if the binary representation is a
palindrome and "false" otherwise, providing a clear indication of whether the input
number's binary form is a palindrome or not.
INTERVIEW
QUESTIONS
Interview questions
1. What is a binary palindrome?

A binary palindrome is a number whose binary representation reads the same

forwards and backwards. For example, the binary representation of the number 9 is

`1001`, which is the same forwards and backwards, making it a binary palindrome.
Interview questions
2. How can you determine if a given integer is a binary palindrome?

To determine if a given integer is a binary palindrome, you can convert the integer to

its binary representation, then check if this binary string reads the same forwards and

backwards. This can be done by comparing the string with its reverse.
Interview questions
3. Why might binary palindromes be important in computer science or specific

applications?

Binary palindromes might be important in certain applications where symmetry and

error-checking are critical. For example, in data transmission and storage, palindromic

patterns can be used to detect and correct errors. Additionally, they have theoretical

importance in the study of algorithms and computational theory.


Interview questions
4. What is the time complexity of checking if a number is a binary palindrome?

The time complexity of checking if a number is a binary palindrome is O(log n), where

n is the given number. This is because converting a number to its binary representation

involves examining each bit, and the number of bits required to represent a number n is

log₂(n).
Interview questions
4. How can bitwise operations be used to check for binary palindromes?

Bitwise operations can be used to check for binary palindromes by extracting bits

from both ends of the binary representation and comparing them. This process involves

using bit shifts and bit masks to isolate and compare the relevant bits.
Practice questions
1. Write a function that checks if the binary representation of a given integer is a

palindrome.

Input: 9 (binary 1001), Output: true

Input: 10 (binary 1010), Output: false


Practice questions
2. Write a function that finds the next integer greater than a given number whose

binary representation is a palindrome.

Input: 9 (binary 1001), Output: 15 (binary 1111)

Input: 10 (binary 1010), Output: 15 (binary 1111)


THANK
YOU

+91 78150 [email protected] www.codemithra.com


95095

You might also like