Open In App

Find the n-th number whose binary representation is a palindrome

Last Updated : 24 May, 2024
Comments
Improve
Suggest changes
21 Likes
Like
Report

Find the nth number whose binary representation is a palindrome. Do not consider the leading zeros, while considering the binary representation. Consider the 1st number whose binary representation is a palindrome as 1, instead of 0 

Examples: 

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)

Method 1: Naive

A naive approach would be, to traverse through all the integers from 1 to 2^31 - 1 and increment the palindrome count, if the number is a palindrome. When the palindrome count reaches the required n, break the loop and return the current integer. 

C++
C Java Python C# JavaScript PHP

Output
27


Time complexity: O(x) where x is a resultant number. 
Auxiliary Space: O(1), the space complexity is O(1) since we are only using a constant amount of memory to store the values.

Method 2: Using BFS

In this approach first, we simply add   "11" this string into the queue. And then for every string, we have two cases. i.e 

  1. if the curr string of even length then add "0" and "1" at the mid of curr string and add it into the queue.
  2. if the curr string is of odd length then add mid char of the curr string into the resultant string and then add it into the queue.
if curr string is of even length
if curr string is of even length
if curr string is of odd length


 Below is the implementation of the above approach:

C++
Java Python C# JavaScript

Output
27

Time complexity: O(N)
Auxiliary Space: O(N)

Method 3: Constructing the nth palindrome

We can construct the nth binary palindrome in its binary representation directly using the below approach. 
If we observe the first few binary palindromes 

 *         | nth Binary  |
n | Palindrome | Group
| |
--------------------------- Group 0
1 ---> 1 (1)


Group 1 (Will have binary representation of length 2*(1)
and 2*(1) + 1)

Fix the first and last bit as 1 and insert nothing
(|) in between. Length is 2*(1)
2 ---> 1|1 (3)

Fix the first and last bit as 1 and insert bit 0
in between. Length is 2*(1) + 1
3 ---> 101 (5)

Fix the first and last bit as 1 and insert bit 1
in between. Length is 2*(1) + 1
4 ---> 111 (7)
F

Group 2 (Will have binary representation of length
2*(2) and 2*(2) + 1). Fix the first and last
bit as 1 and insert nothing (|) at middle.
And put 0 in binary format in both directions
from middle. Length is 2*(2)
5 ---> 10|01
Fix the first and last bit as 1 and insert
nothing (|) at middle. And put 1 in binary
format in both directions from middle.
Length is 2*(2)
6 ---> 11|11

7 ---> 10001
8 ---> 10101
9 ---> 11011
10 ---> 11111

Group 3 (Will have binary representation of
length 2*(3) and 2*(3) + 1)
11 ---> 100|001
12 ---> 101|101
13 ---> 110|011
14 ---> 111|111

15 ---> 1000001
16 ---> 1001001
17 ---> 1010101
18 ---> 1011101
19 ---> 1100011
20 ---> 1101011
21 ---> 1110111
22 ---> 1111111
--------------------

Algorithm: 
1) We can divide the set of palindrome numbers into some groups. 
2) n-th group will have (2^(n-1) + 2^n = 3 * 2 ^(n-1) ) number of binary palindromes 
3) With the given number, we can find the group to which it belongs and the offset in that group. 
4) As the leading zeros are not to be considered, we should use bit 1 as the starting bit and the ending bit of the number in binary representation 
5) And we will fill other bits based on the groupno and groupoffset 
6) Based on the offset, we can find which bit should be inserted at the middle (|(nothing) or 0 or 1) and 
which number(in binary form) (1 or 2 or 3 or 4 or ..) should be placed in both directions from the middle

Consider Below Example 

Let us construct the 8th binary palindrome number
The group number will be 2, and no.of elements
before that group are 1 + 3 * 2^1 which is 4

So the offset for the 8th element will be 8 - 4
- 1 = 3

And first 2^(groupno - 1) = 2^1, elements will
have even length(in binary representation) of
2*groupno, next 2^groupno elements will have odd
length(in binary representation) of 2*groupno + 1

Place bit 1 as the first bit and as the last bit
(firstbit: 0, last bit: 2*groupno or 2*groupno - 1)

As the offset is 3, 4th(3 + 1) element in the
group, will have odd length and have 1 in the
middle

Below is the table of middle bit to be used for
the given offset for the group 2
offset middle bit
0 |
1 |
2 0
3 1
4 0
5 1
And we should be filling the binary representation
of number 0(((groupoffset) - 2^(groupno-1)) /2)
from middle n both directions
1 0 1 0 1
FirstElement Number MiddleElement Number LastElement
1 0 1 0 1

The 8th number will be 21

Below is the implementation of the above idea : 

C++
C Java Python C# JavaScript PHP

Output
27

Time Complexity: O(1).
Auxiliary Space: O(1)

Reference: 
https://www.codeproject.com/Articles/1162038/Finding-nth-Binary-Palindrome-in-Csharp
See your article appearing on GeeksforGeek's main page and help other Geeks.


Explore