VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-n-th-number-whose-binary-representation-palindrome/

⇱ Nth Binary Palindrome - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Nth Binary Palindrome

Last Updated : 11 Apr, 2026

0Find the nth number whose binary representation is a palindrome, ignoring leading zeros, with the sequence starting from 1 (not 0).

Examples:

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

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

[Naive Approach] Iterative Method

Iterate through all integers starting from 1 and examine their binary representation using bit operations. Compare bits from both ends to determine if the number forms a palindrome. Count each valid case and return the number when the count reaches n.

  • Start iterating from i = 1 and check each number one by one.
  • For each number, find its binary length using leftmostSetBit().
  • Compare bits using two pointers (l from left, r from right).
  • Check bits using isKthBitSet() and move inward if equal.
  • If all bits match, increment palindrome count.
  • Stop when count reaches n and return number.

Output
27


Time complexity: O(x * log x ), Iterate up to the nth palindrome number (x), and each check takes O(log x) time due to bit comparison.

Auxiliary Space: O(1).

[Better Approach] Using BFS - O(n) Time and O(n) Space

Use BFS (queue) to generate binary palindromes level by level starting from "11". At each step, expand the current string by inserting bits at the middle to form new palindromes. Continue this level-wise generation until the nth palindrome is reached and return its integer value.

  • Initialize queue with "11".
  • Pop string, decrement n, and process it.
  • If n == 0, return current string converted to integer.
  • If even length, insert "0" and "1" at middle and push.
  • If odd length, duplicate middle character and push.
  • Repeat until nth palindrome is reached.
👁 11

Output
27

[Expected Approach] Constructing the nth palindrome - O(1) Time and O(1) Space

If we observe the first few binary palindromes it can be divided into groups based on their length:

  • Group 0: 1
  • Group 1: 11, 101, 111
  • Group 2: 1001, 1111, 10001, 10101, 11011, 11111
  • Group 3: 100001, 101101, 110011, 111111, ...

Group 1 is formed by taking two 1s at the corner and filling 0 or 1 bits in between
Group 2 is formed by taking two 1s at the corner and filling 2 or 3 bits in between
Group 3 is formed by taking two 1s at the corner and filling 4 or 5 bits in between

The number of palindromes in a group is given by 3 * 2^(groupNo - 1) for groupNo >= 1, and for groupNo = 0, there is only 1 palindrome.

Now, directly construct the nth binary palindrome using group and offset. Use symmetry to fix outer bits and determine middle and inner bits.

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


Output
27
Comment