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

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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Method 1: Naive

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

## C++

 `// C++ program to find n-th number whose binary ` `// representation is palindrome. ` `#include ` `using` `namespace` `std; ` ` `  `/* Finds if the kth bit is set in the binary  ` `representation */` `int` `isKthBitSet(``int` `x, ``int` `k) ` `{ ` `    ``return` `(x & (1 << (k - 1))) ? 1 : 0; ` `} ` ` `  `/* Returns the position of leftmost set bit  ` `in the binary representation */` `int` `leftmostSetBit(``int` `x) ` `{ ` `    ``int` `count = 0; ` `    ``while` `(x) { ` `        ``count++; ` `        ``x = x >> 1; ` `    ``} ` `    ``return` `count; ` `} ` ` `  `/* Finds whether the integer in binary  ` `representation is palindrome or not*/` `int` `isBinPalindrome(``int` `x) ` `{ ` `    ``int` `l = leftmostSetBit(x); ` `    ``int` `r = 1; ` ` `  `    ``// One by one compare bits ` `    ``while` `(l > r) { ` ` `  `        ``// Compare left and right bits and converge ` `        ``if` `(isKthBitSet(x, l) != isKthBitSet(x, r)) ` `            ``return` `0; ` `        ``l--; ` `        ``r++; ` `    ``} ` `    ``return` `1; ` `} ` ` `  `int` `findNthPalindrome(``int` `n) ` `{ ` `    ``int` `pal_count = 0; ` ` `  `    ``/* Start from 1, traverse through  ` `    ``all the integers */` `    ``int` `i = 0; ` `    ``for` `(i = 1; i <= INT_MAX; i++) { ` `        ``if` `(isBinPalindrome(i)) { ` `            ``pal_count++; ` `        ``} ` `        ``/* If we reach n, break the loop */` `        ``if` `(pal_count == n) ` `            ``break``; ` `    ``} ` ` `  `    ``return` `i; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 9; ` `    ``cout << findNthPalindrome(n); ` `} ` ` `  `// This code is contributed ` `// by Akanksha Rai `

## C

 `// C program to find n-th number whose binary ` `// representation is palindrome. ` `#include ` `#define INT_MAX 2147483647 ` ` `  `/* Finds if the kth bit is set in the binary  ` `   ``representation */` `int` `isKthBitSet(``int` `x, ``int` `k) ` `{ ` `    ``return` `(x & (1 << (k - 1))) ? 1 : 0; ` `} ` ` `  `/* Returns the position of leftmost set bit  ` `   ``in the binary representation */` `int` `leftmostSetBit(``int` `x) ` `{ ` `    ``int` `count = 0; ` `    ``while` `(x) { ` `        ``count++; ` `        ``x = x >> 1; ` `    ``} ` `    ``return` `count; ` `} ` ` `  `/* Finds whether the integer in binary  ` `   ``representation is palindrome or not*/` `int` `isBinPalindrome(``int` `x) ` `{ ` `    ``int` `l = leftmostSetBit(x); ` `    ``int` `r = 1; ` ` `  `    ``// One by one compare bits ` `    ``while` `(l > r) { ` ` `  `        ``// Compare left and right bits and converge ` `        ``if` `(isKthBitSet(x, l) != isKthBitSet(x, r)) ` `            ``return` `0; ` `        ``l--; ` `        ``r++; ` `    ``} ` `    ``return` `1; ` `} ` ` `  `int` `findNthPalindrome(``int` `n) ` `{ ` `    ``int` `pal_count = 0; ` ` `  `    ``/* Start from 1, traverse through  ` `      ``all the integers */` `    ``int` `i = 0; ` `    ``for` `(i = 1; i <= INT_MAX; i++) { ` `        ``if` `(isBinPalindrome(i)) { ` `            ``pal_count++; ` `        ``} ` `        ``/* If we reach n, break the loop */` `        ``if` `(pal_count == n) ` `            ``break``; ` `    ``} ` ` `  `    ``return` `i; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 9; ` `    ``printf``(``"%d"``, findNthPalindrome(n)); ` `} `

## Java

 `// Java program to find n-th  ` `// number whose binary  ` `// representation is palindrome. ` `import` `java.io.*; ` ` `  `class` `GFG  ` `{ ` `static` `int` `INT_MAX = ``2147483647``; ` ` `  `/* Finds if the kth bit  ` `is set in the binary  ` `representation */` `static` `int` `isKthBitSet(``int` `x, ``int` `k) ` `{ ` `    ``return` `((x & (``1` `<<  ` `            ``(k - ``1``))) > ``0``) ? ``1` `: ``0``; ` `} ` ` `  `/* Returns the position of  ` `leftmost set bit in the ` `binary representation */` `static` `int` `leftmostSetBit(``int` `x) ` `{ ` `    ``int` `count = ``0``; ` `    ``while` `(x > ``0``) ` `    ``{ ` `        ``count++; ` `        ``x = x >> ``1``; ` `    ``} ` `    ``return` `count; ` `} ` ` `  `/* Finds whether the integer  ` `in binary representation is  ` `palindrome or not*/` `static` `int` `isBinPalindrome(``int` `x) ` `{ ` `    ``int` `l = leftmostSetBit(x); ` `    ``int` `r = ``1``; ` ` `  `    ``// One by one compare bits ` `    ``while` `(l > r)  ` `    ``{ ` ` `  `        ``// Compare left and right ` `        ``// bits and converge ` `        ``if` `(isKthBitSet(x, l) !=  ` `            ``isKthBitSet(x, r)) ` `            ``return` `0``; ` `        ``l--; ` `        ``r++; ` `    ``} ` `    ``return` `1``; ` `} ` ` `  `static` `int` `findNthPalindrome(``int` `n) ` `{ ` `    ``int` `pal_count = ``0``; ` ` `  `    ``/* Start from 1, traverse  ` `    ``through all the integers */` `    ``int` `i = ``0``; ` `    ``for` `(i = ``1``; i <= INT_MAX; i++)  ` `    ``{ ` `        ``if` `(isBinPalindrome(i) > ``0``)  ` `        ``{ ` `            ``pal_count++; ` `        ``} ` `         `  `        ``/* If we reach n,  ` `        ``break the loop */` `        ``if` `(pal_count == n) ` `            ``break``; ` `    ``} ` ` `  `    ``return` `i; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main (String[] args)  ` `{ ` `    ``int` `n = ``9``; ` `    ``System.out.println(findNthPalindrome(n)); ` `} ` `} ` ` `  `// This code is contributed  ` `// by anuj_67. `

## Python 3

 `# Python 3 program to find n-th number  ` `# whose binary representation is palindrome. ` `INT_MAX ``=` `2147483647` ` `  `# Finds if the kth bit is set in  ` `# the binary representation  ` `def` `isKthBitSet(x, k): ` ` `  `    ``return` `1` `if` `(x & (``1` `<< (k ``-` `1``))) ``else` `0` ` `  `# Returns the position of leftmost  ` `# set bit in the binary representation  ` `def` `leftmostSetBit(x): ` ` `  `    ``count ``=` `0` `    ``while` `(x) : ` `        ``count ``+``=` `1` `        ``x ``=` `x >> ``1` `     `  `    ``return` `count ` ` `  `# Finds whether the integer in binary  ` `# representation is palindrome or not ` `def` `isBinPalindrome(x): ` ` `  `    ``l ``=` `leftmostSetBit(x) ` `    ``r ``=` `1` ` `  `    ``# One by one compare bits ` `    ``while` `(l > r) : ` ` `  `        ``# Compare left and right bits  ` `        ``# and converge ` `        ``if` `(isKthBitSet(x, l) !``=` `isKthBitSet(x, r)): ` `            ``return` `0` `        ``l ``-``=` `1` `        ``r ``+``=` `1` `    ``return` `1` ` `  `def` `findNthPalindrome(n): ` `    ``pal_count ``=` `0` ` `  `    ``# Start from 1, traverse   ` `    ``# through all the integers  ` `    ``i ``=` `0` `    ``for` `i ``in` `range``( ``1``, INT_MAX ``+` `1``) : ` `        ``if` `(isBinPalindrome(i)) : ` `            ``pal_count ``+``=` `1` `     `  `        ``# If we reach n, break the loop  ` `        ``if` `(pal_count ``=``=` `n): ` `            ``break` ` `  `    ``return` `i ` ` `  `# Driver code ` `if` `__name__ ``=``=` `"__main__"``: ` `    ``n ``=` `9` `    ``print``(findNthPalindrome(n)) ` ` `  `# This code is contributed  ` `# by ChitraNayal `

## C#

 `// C# program to find n-th  ` `// number whose binary  ` `// representation is palindrome. ` `using` `System; ` ` `  `class` `GFG { ` `     `  `static` `int` `INT_MAX = 2147483647;  ` ` `  `/* Finds if the kth bit  ` `   ``is set in the binary  ` `   ``representation */` `static` `int` `isKthBitSet(``int` `x, ``int` `k)  ` `{  ` `    ``return` `((x & (1 <<  ` `            ``(k - 1))) > 0) ? 1 : 0;  ` `}  ` ` `  `/* Returns the position of  ` `   ``leftmost set bit in the  ` `   ``binary representation */` `static` `int` `leftmostSetBit(``int` `x)  ` `{  ` `    ``int` `count = 0;  ` `    ``while` `(x > 0)  ` `    ``{  ` `        ``count++;  ` `        ``x = x >> 1;  ` `    ``}  ` `    ``return` `count;  ` `}  ` ` `  `/* Finds whether the integer  ` `   ``in binary representation is  ` `   ``palindrome or not */` `static` `int` `isBinPalindrome(``int` `x)  ` `{  ` `    ``int` `l = leftmostSetBit(x);  ` `    ``int` `r = 1;  ` ` `  `    ``// One by one compare bits  ` `    ``while` `(l > r)  ` `    ``{  ` ` `  `        ``// Compare left and right  ` `        ``// bits and converge  ` `        ``if` `(isKthBitSet(x, l) !=  ` `            ``isKthBitSet(x, r))  ` `            ``return` `0;  ` `        ``l--;  ` `        ``r++;  ` `    ``}  ` `    ``return` `1;  ` `}  ` ` `  `static` `int` `findNthPalindrome(``int` `n)  ` `{  ` `    ``int` `pal_count = 0;  ` ` `  `    ``/* Start from 1, traverse  ` `       ``through all the integers */` `    ``int` `i = 0;  ` `    ``for` `(i = 1; i <= INT_MAX; i++)  ` `    ``{  ` `        ``if` `(isBinPalindrome(i) > 0)  ` `        ``{  ` `            ``pal_count++;  ` `        ``}  ` `         `  `        ``/* If we reach n,  ` `        ``break the loop */` `        ``if` `(pal_count == n)  ` `            ``break``;  ` `    ``}  ` ` `  `    ``return` `i;  ` `}  ` ` `  `// Driver code  ` `static` `public` `void` `Main () ` `{ ` `    ``int` `n = 9;  ` `    ``Console.WriteLine(findNthPalindrome(n));  ` `}  ` `}  ` ` `  `// This code is contributed ajit `

## PHP

 `> 1; ` `    ``} ` `    ``return` `\$count``; ` `} ` ` `  `// Finds whether the integer in binary  ` `// representation is palindrome or not ` `function` `isBinPalindrome(``\$x``) ` `{ ` `    ``\$l` `= leftmostSetBit(``\$x``); ` `    ``\$r` `= 1; ` ` `  `    ``// One by one compare bits ` `    ``while` `(``\$l` `> ``\$r``)  ` `    ``{ ` ` `  `        ``// Compare left and right bits ` `        ``// and converge ` `        ``if` `(isKthBitSet(``\$x``, ``\$l``) !=  ` `            ``isKthBitSet(``\$x``, ``\$r``)) ` `            ``return` `0; ` `        ``\$l``--; ` `        ``\$r``++; ` `    ``} ` `    ``return` `1; ` `} ` ` `  `function` `findNthPalindrome(``\$n``) ` `{ ` `    ``\$pal_count` `= 0; ` ` `  `    ``// Start from 1, traverse through  ` `    ``// all the integers  ` `    ``\$i` `= 0; ` `    ``for` `(``\$i` `= 1; ``\$i` `<= PHP_INT_MAX; ``\$i``++)  ` `    ``{ ` `        ``if` `(isBinPalindrome(``\$i``))  ` `        ``{ ` `            ``\$pal_count``++; ` `        ``} ` `         `  `        ``/* If we reach n, break the loop */` `        ``if` `(``\$pal_count` `== ``\$n``) ` `            ``break``; ` `    ``} ` `    ``return` `\$i``; ` `} ` ` `  `// Driver code ` `\$n` `= 9; ` `echo` `(findNthPalindrome(``\$n``)); ` ` `  `// This code is contributed by jit_t ` `?> `

Output:

```27
```

Time complexity of this solution is O(x) where x is resultant number. Note that value of x is generally much larger than n.

Method 2: Constructing the nth palindrome

We can construct the nth binary palindrome in its binary representation directly using the below approach.
If we observe 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 to 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 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 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
```

## C

 `// Efficient C program to find n-th palindrome ` `#include ` `#define INT_SIZE 32 ` ` `  `/* Construct the nth binary palindrome with the ` `   ``given group number, aux_number and operation ` `   ``type */` `int` `constructNthNumber(``int` `group_no, ``int` `aux_num, ` `                                          ``int` `op) ` `{ ` `    ``int` `a[INT_SIZE] = {0}; ` ` `  `    ``int` `num = 0, len_f; ` `    ``int` `i = 0; ` ` `  `     ``// No need to insert any bit in the middle ` `    ``if` `(op == 2) ` `    ``{ ` `        ``// Length of the final binary representation ` `        ``len_f = 2 * group_no; ` ` `  `        ``// Fill first and last bit as 1 ` `        ``a[len_f - 1] = a = 1; ` ` `  `        ``/* Start filling the a[] from middle, ` `           ``with the aux_num binary representation */` `        ``while` `(aux_num) ` `        ``{ ` `            ``// Get the auxiliary number's ith bit and ` `            ``// fill around middle ` `            ``a[group_no + i] = a[group_no - 1 - i] ` `                            ``= aux_num & 1; ` `            ``aux_num = aux_num >> 1; ` `            ``i++; ` `        ``} ` `    ``} ` ` `  `    ``// Insert bit 0 in the middle ` `    ``else` `if` `(op == 0) ` `    ``{ ` `        ``// Length of the final binary representation ` `        ``len_f = 2 * group_no + 1; ` ` `  `        ``// Fill first and last bit as 1 ` `        ``a[len_f - 1] = a = 1; ` `        ``a[group_no] = 0; ` ` `  `        ``/* Start filling the a[] from middle, with ` `        ``the aux_num binary representation */` `        ``while` `(aux_num) ` `        ``{ ` `            ``// Get the auxiliary number's ith bit and fill ` `            ``// around middle ` `            ``a[group_no + 1 + i] = a[group_no - 1 - i] ` `                                ``= aux_num & 1; ` `            ``aux_num = aux_num >> 1; ` `            ``i++; ` `        ``} ` `    ``} ` `    ``else`     `// Insert bit 1 in the middle ` `    ``{ ` `        ``// Length of the final binary representation ` `        ``len_f = 2 * group_no + 1; ` ` `  `        ``// Fill first and last bit as 1 ` `        ``a[len_f - 1] = a = 1; ` `        ``a[group_no] = 1; ` ` `  `        ``/* Start filling the a[] from middle, with ` `           ``the aux_num binary representation */` `        ``while` `(aux_num) ` `        ``{ ` `            ``// Get the auxiliary number's ith bit and fill ` `            ``// around middle ` `            ``a[group_no + 1 + i] = a[group_no - 1 - i] ` `                                ``= aux_num & 1; ` `            ``aux_num = aux_num >> 1; ` `            ``i++; ` `        ``} ` `    ``} ` ` `  `    ``/* Convert the number to decimal from binary */` `    ``for` `(i = 0; i < len_f; i++) ` `        ``num += (1 << i) * a[i]; ` `    ``return` `num; ` `} ` ` `  `/* Will return the nth binary palindrome number */` `int` `getNthNumber(``int` `n) ` `{ ` `    ``int` `group_no = 0, group_offset; ` `    ``int` `count_upto_group = 0, count_temp = 1; ` `    ``int` `op, aux_num; ` ` `  `    ``/* Add number of elements in all the groups, ` `       ``until the group of the nth number is found */` `    ``while` `(count_temp < n) ` `    ``{ ` `        ``group_no++; ` ` `  `        ``// Total number of elements until this group ` `        ``count_upto_group = count_temp; ` `        ``count_temp += 3 * (1 << (group_no - 1)); ` `    ``} ` ` `  `    ``// Element's offset position in the group ` `    ``group_offset = n - count_upto_group - 1; ` ` `  `    ``/* Finding which bit to be placed in the ` `       ``middle and finding the number, which we ` `       ``will fill from the middle in both ` `       ``directions */` `    ``if` `((group_offset + 1) <= (1 << (group_no - 1))) ` `    ``{ ` `        ``op = 2; ``// No need to put extra bit in middle ` ` `  `        ``// We need to fill this auxiliary number ` `        ``// in binary form the middle in both directions ` `        ``aux_num = group_offset; ` `    ``} ` `    ``else` `    ``{ ` `        ``if` `(((group_offset + 1) - (1 << (group_no - 1))) % 2) ` `            ``op = 0; ``// Need to Insert 0 at middle ` `        ``else` `            ``op = 1; ``// Need to Insert 1 at middle ` `        ``aux_num = ((group_offset) - (1 << (group_no - 1))) / 2; ` `    ``} ` ` `  `    ``return` `constructNthNumber(group_no, aux_num, op); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 9; ` `    ``printf``(``"%d"``, getNthNumber(n)); ` `    ``return` `0; ` `} `

## Java

 `// Efficient Java program to find n-th palindrome ` `class` `GFG ` `{ ` `     `  `static` `int` `INT_SIZE = ``32``; ` ` `  `/* Construct the nth binary palindrome with the ` `given group number, aux_number and operation ` `type */` `static` `int` `constructNthNumber(``int` `group_no, ``int` `aux_num, ` `                                        ``int` `op) ` `{ ` `    ``int` `a[] = ``new` `int``[INT_SIZE]; ` ` `  `    ``int` `num = ``0``, len_f; ` `    ``int` `i = ``0``; ` ` `  `    ``// No need to insert any bit in the middle ` `    ``if` `(op == ``2``) ` `    ``{ ` `        ``// Length of the final binary representation ` `        ``len_f = ``2` `* group_no; ` ` `  `        ``// Fill first and last bit as 1 ` `        ``a[len_f - ``1``] = a[``0``] = ``1``; ` ` `  `        ``/* Start filling the a[] from middle, ` `        ``with the aux_num binary representation */` `        ``while` `(aux_num > ``0``) ` `        ``{ ` `            ``// Get the auxiliary number's ith bit and ` `            ``// fill around middle ` `            ``a[group_no + i] = a[group_no - ``1` `- i] ` `                            ``= aux_num & ``1``; ` `            ``aux_num = aux_num >> ``1``; ` `            ``i++; ` `        ``} ` `    ``} ` ` `  `    ``// Insert bit 0 in the middle ` `    ``else` `if` `(op == ``0``) ` `    ``{ ` `        ``// Length of the final binary representation ` `        ``len_f = ``2` `* group_no + ``1``; ` ` `  `        ``// Fill first and last bit as 1 ` `        ``a[len_f - ``1``] = a[``0``] = ``1``; ` `        ``a[group_no] = ``0``; ` ` `  `        ``/* Start filling the a[] from middle, with ` `        ``the aux_num binary representation */` `        ``while` `(aux_num > ``0``) ` `        ``{ ` `            ``// Get the auxiliary number's ith bit and fill ` `            ``// around middle ` `            ``a[group_no + ``1` `+ i] = a[group_no - ``1` `- i] ` `                                ``= aux_num & ``1``; ` `            ``aux_num = aux_num >> ``1``; ` `            ``i++; ` `        ``} ` `    ``} ` `    ``else`     `// Insert bit 1 in the middle ` `    ``{ ` `        ``// Length of the final binary representation ` `        ``len_f = ``2` `* group_no + ``1``; ` ` `  `        ``// Fill first and last bit as 1 ` `        ``a[len_f - ``1``] = a[``0``] = ``1``; ` `        ``a[group_no] = ``1``; ` ` `  `        ``/* Start filling the a[] from middle, with ` `        ``the aux_num binary representation */` `        ``while` `(aux_num>``0``) ` `        ``{ ` `            ``// Get the auxiliary number's ith bit and fill ` `            ``// around middle ` `            ``a[group_no + ``1` `+ i] = a[group_no - ``1` `- i] ` `                                ``= aux_num & ``1``; ` `            ``aux_num = aux_num >> ``1``; ` `            ``i++; ` `        ``} ` `    ``} ` ` `  `    ``/* Convert the number to decimal from binary */` `    ``for` `(i = ``0``; i < len_f; i++) ` `        ``num += (``1` `<< i) * a[i]; ` `    ``return` `num; ` `} ` ` `  `/* Will return the nth binary palindrome number */` `static` `int` `getNthNumber(``int` `n) ` `{ ` `    ``int` `group_no = ``0``, group_offset; ` `    ``int` `count_upto_group = ``0``, count_temp = ``1``; ` `    ``int` `op, aux_num; ` ` `  `    ``/* Add number of elements in all the groups, ` `    ``until the group of the nth number is found */` `    ``while` `(count_temp < n) ` `    ``{ ` `        ``group_no++; ` ` `  `        ``// Total number of elements until this group ` `        ``count_upto_group = count_temp; ` `        ``count_temp += ``3` `* (``1` `<< (group_no - ``1``)); ` `    ``} ` ` `  `    ``// Element's offset position in the group ` `    ``group_offset = n - count_upto_group - ``1``; ` ` `  `    ``/* Finding which bit to be placed in the ` `    ``middle and finding the number, which we ` `    ``will fill from the middle in both ` `    ``directions */` `    ``if` `((group_offset + ``1``) <= (``1` `<< (group_no - ``1``))) ` `    ``{ ` `        ``op = ``2``; ``// No need to put extra bit in middle ` ` `  `        ``// We need to fill this auxiliary number ` `        ``// in binary form the middle in both directions ` `        ``aux_num = group_offset; ` `    ``} ` `    ``else` `    ``{ ` `        ``if` `(((group_offset + ``1``) - (``1` `<< (group_no - ``1``))) % ``2``==``1``) ` `            ``op = ``0``; ``// Need to Insert 0 at middle ` `        ``else` `            ``op = ``1``; ``// Need to Insert 1 at middle ` `        ``aux_num = ((group_offset) - (``1` `<< (group_no - ``1``))) / ``2``; ` `    ``} ` ` `  `    ``return` `constructNthNumber(group_no, aux_num, op); ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args)  ` `{ ` `    ``int` `n = ``9``; ` `    ``System.out.printf(``"%d"``, getNthNumber(n)); ` `} ` `} ` ` `  `/* This code contributed by PrinciRaj1992 */`

## Python3

 `# Efficient Python3 program to find n-th palindrome  ` `INT_SIZE ``=` `32` ` `  `# Construct the nth binary palindrome with the  ` `# given group number, aux_number and operation type ` `def` `constructNthNumber(group_no, aux_num, op):  ` ` `  `    ``a ``=` `[``0``] ``*` `INT_SIZE  ` `    ``num, i ``=` `0``, ``0` `     `  `    ``# No need to insert any bit in the middle  ` `    ``if` `op ``=``=` `2``:  ` `     `  `        ``# Length of the final binary representation  ` `        ``len_f ``=` `2` `*` `group_no  ` ` `  `        ``# Fill first and last bit as 1  ` `        ``a[len_f ``-` `1``] ``=` `a[``0``] ``=` `1` ` `  `        ``# Start filling the a[] from middle,  ` `        ``# with the aux_num binary representation  ` `        ``while` `aux_num:  ` `         `  `            ``# Get the auxiliary number's ith  ` `            ``# bit and fill around middle  ` `            ``a[group_no ``+` `i] ``=` `a[group_no ``-` `1` `-` `i] ``=` `\ ` `                                     ``aux_num & ``1` `            ``aux_num ``=` `aux_num >> ``1` `            ``i ``+``=` `1` ` `  `    ``# Insert bit 0 in the middle  ` `    ``elif` `op ``=``=` `0``:  ` `     `  `        ``# Length of the final binary representation  ` `        ``len_f ``=` `2` `*` `group_no ``+` `1` ` `  `        ``# Fill first and last bit as 1  ` `        ``a[len_f ``-` `1``] ``=` `a[``0``] ``=` `1` `        ``a[group_no] ``=` `0` ` `  `        ``# Start filling the a[] from middle, with  ` `        ``# the aux_num binary representation ` `        ``while` `aux_num:  ` `         `  `            ``# Get the auxiliary number's ith  ` `            ``# bit and fill around middle  ` `            ``a[group_no ``+` `1` `+` `i] ``=` `a[group_no ``-` `1` `-` `i] ``=` `\ ` `                                         ``aux_num & ``1` `            ``aux_num ``=` `aux_num >> ``1` `            ``i ``+``=` `1` `         `  `    ``else``: ``# Insert bit 1 in the middle  ` `     `  `        ``# Length of the final binary representation  ` `        ``len_f ``=` `2` `*` `group_no ``+` `1` ` `  `        ``# Fill first and last bit as 1  ` `        ``a[len_f ``-` `1``] ``=` `a[``0``] ``=` `1` `        ``a[group_no] ``=` `1` ` `  `        ``# Start filling the a[] from middle, with  ` `        ``# the aux_num binary representation  ` `        ``while` `aux_num: ` `         `  `            ``# Get the auxiliary number's ith  ` `            ``# bit and fill around middle  ` `            ``a[group_no ``+` `1` `+` `i] ``=` `a[group_no ``-` `1` `-` `i] ``=` `\ ` `                                         ``aux_num & ``1` `            ``aux_num ``=` `aux_num >> ``1` `            ``i ``+``=` `1` ` `  `    ``# Convert the number to decimal from binary  ` `    ``for` `i ``in` `range``(``0``, len_f):  ` `        ``num ``+``=` `(``1` `<< i) ``*` `a[i]  ` `    ``return` `num  ` ` `  `# Will return the nth binary palindrome number  ` `def` `getNthNumber(n): ` ` `  `    ``group_no ``=` `0` `    ``count_upto_group, count_temp ``=` `0``, ``1` `     `  `    ``# Add number of elements in all the groups,  ` `    ``# until the group of the nth number is found ` `    ``while` `count_temp < n: ` `     `  `        ``group_no ``+``=` `1` ` `  `        ``# Total number of elements until this group  ` `        ``count_upto_group ``=` `count_temp  ` `        ``count_temp ``+``=` `3` `*` `(``1` `<< (group_no ``-` `1``))  ` ` `  `    ``# Element's offset position in the group  ` `    ``group_offset ``=` `n ``-` `count_upto_group ``-` `1` ` `  `    ``# Finding which bit to be placed in the  ` `    ``# middle and finding the number, which we  ` `    ``# will fill from the middle in both directions  ` `    ``if` `(group_offset ``+` `1``) <``=` `(``1` `<< (group_no ``-` `1``)): ` `     `  `        ``op ``=` `2` `# No need to put extra bit in middle  ` ` `  `        ``# We need to fill this auxiliary number  ` `        ``# in binary form the middle in both directions  ` `        ``aux_num ``=` `group_offset  ` `     `  `    ``else``: ` `     `  `        ``if` `(((group_offset ``+` `1``) ``-`  `             ``(``1` `<< (group_no ``-` `1``))) ``%` `2``):  ` `            ``op ``=` `0` `# Need to Insert 0 at middle  ` `        ``else``: ` `            ``op ``=` `1` `# Need to Insert 1 at middle  ` `        ``aux_num ``=` `(((group_offset) ``-`  `                    ``(``1` `<< (group_no ``-` `1``))) ``/``/` `2``) ` `     `  `    ``return` `constructNthNumber(group_no, aux_num, op)  ` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"``: ` ` `  `    ``n ``=` `9` `    ``print``(getNthNumber(n))  ` `     `  `# This code is contributed by Rituraj Jain `

## C#

 `// Efficient C# program to find n-th palindrome ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `static` `int` `INT_SIZE = 32; ` ` `  `/* Construct the nth binary palindrome with the ` `given group number, aux_number and operation ` `type */` `static` `int` `constructNthNumber(``int` `group_no, ``int` `aux_num, ` `                                        ``int` `op) ` `{ ` `    ``int` `[]a = ``new` `int``[INT_SIZE]; ` ` `  `    ``int` `num = 0, len_f; ` `    ``int` `i = 0; ` ` `  `    ``// No need to insert any bit in the middle ` `    ``if` `(op == 2) ` `    ``{ ` `        ``// Length of the final binary representation ` `        ``len_f = 2 * group_no; ` ` `  `        ``// Fill first and last bit as 1 ` `        ``a[len_f - 1] = a = 1; ` ` `  `        ``/* Start filling the a[] from middle, ` `        ``with the aux_num binary representation */` `        ``while` `(aux_num > 0) ` `        ``{ ` `            ``// Get the auxiliary number's ith bit and ` `            ``// fill around middle ` `            ``a[group_no + i] = a[group_no - 1 - i] ` `                            ``= aux_num & 1; ` `            ``aux_num = aux_num >> 1; ` `            ``i++; ` `        ``} ` `    ``} ` ` `  `    ``// Insert bit 0 in the middle ` `    ``else` `if` `(op == 0) ` `    ``{ ` `        ``// Length of the final binary representation ` `        ``len_f = 2 * group_no + 1; ` ` `  `        ``// Fill first and last bit as 1 ` `        ``a[len_f - 1] = a = 1; ` `        ``a[group_no] = 0; ` ` `  `        ``/* Start filling the a[] from middle, with ` `        ``the aux_num binary representation */` `        ``while` `(aux_num > 0) ` `        ``{ ` `            ``// Get the auxiliary number's ith bit and fill ` `            ``// around middle ` `            ``a[group_no + 1 + i] = a[group_no - 1 - i] ` `                                ``= aux_num & 1; ` `            ``aux_num = aux_num >> 1; ` `            ``i++; ` `        ``} ` `    ``} ` `    ``else`     `// Insert bit 1 in the middle ` `    ``{ ` `        ``// Length of the final binary representation ` `        ``len_f = 2 * group_no + 1; ` ` `  `        ``// Fill first and last bit as 1 ` `        ``a[len_f - 1] = a = 1; ` `        ``a[group_no] = 1; ` ` `  `        ``/* Start filling the a[] from middle, with ` `        ``the aux_num binary representation */` `        ``while` `(aux_num>0) ` `        ``{ ` `            ``// Get the auxiliary number's ith bit and fill ` `            ``// around middle ` `            ``a[group_no + 1 + i] = a[group_no - 1 - i] ` `                                ``= aux_num & 1; ` `            ``aux_num = aux_num >> 1; ` `            ``i++; ` `        ``} ` `    ``} ` ` `  `    ``/* Convert the number to decimal from binary */` `    ``for` `(i = 0; i < len_f; i++) ` `        ``num += (1 << i) * a[i]; ` `    ``return` `num; ` `} ` ` `  `/* Will return the nth binary palindrome number */` `static` `int` `getNthNumber(``int` `n) ` `{ ` `    ``int` `group_no = 0, group_offset; ` `    ``int` `count_upto_group = 0, count_temp = 1; ` `    ``int` `op, aux_num; ` ` `  `    ``/* Add number of elements in all the groups, ` `    ``until the group of the nth number is found */` `    ``while` `(count_temp < n) ` `    ``{ ` `        ``group_no++; ` ` `  `        ``// Total number of elements until this group ` `        ``count_upto_group = count_temp; ` `        ``count_temp += 3 * (1 << (group_no - 1)); ` `    ``} ` ` `  `    ``// Element's offset position in the group ` `    ``group_offset = n - count_upto_group - 1; ` ` `  `    ``/* Finding which bit to be placed in the ` `    ``middle and finding the number, which we ` `    ``will fill from the middle in both ` `    ``directions */` `    ``if` `((group_offset + 1) <= (1 << (group_no - 1))) ` `    ``{ ` `        ``op = 2; ``// No need to put extra bit in middle ` ` `  `        ``// We need to fill this auxiliary number ` `        ``// in binary form the middle in both directions ` `        ``aux_num = group_offset; ` `    ``} ` `    ``else` `    ``{ ` `        ``if` `(((group_offset + 1) - (1 << (group_no - 1))) % 2==1) ` `            ``op = 0; ``// Need to Insert 0 at middle ` `        ``else` `            ``op = 1; ``// Need to Insert 1 at middle ` `        ``aux_num = ((group_offset) - (1 << (group_no - 1))) / 2; ` `    ``} ` ` `  `    ``return` `constructNthNumber(group_no, aux_num, op); ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String[] args)  ` `{ ` `    ``int` `n = 9; ` `    ``Console.Write(``"{0}"``, getNthNumber(n)); ` `} ` `} ` ` `  `// This code contributed by Rajput-Ji `

## PHP

 `> 1; ` `            ``\$i``++; ` `        ``} ` `    ``} ` ` `  `    ``// Insert bit 0 in the middle ` `    ``else` `if` `(``\$op` `== 0) ` `    ``{ ` `        ``// Length of the final binary representation ` `        ``\$len_f` `= 2 * ``\$group_no` `+ 1; ` ` `  `        ``// Fill first and last bit as 1 ` `        ``\$a``[``\$len_f` `- 1] = ``\$a`` = 1; ` `        ``\$a``[``\$group_no``] = 0; ` ` `  `        ``/* Start filling the a[] from middle, with ` `        ``the aux_num binary representation */` `        ``while` `(``\$aux_num``) ` `        ``{ ` `            ``// Get the auxiliary number's ith bit and fill ` `            ``// around middle ` `            ``\$a``[``\$group_no` `+ 1 + ``\$i``] = ``\$a``[``\$group_no` `- 1 - ``\$i``] ` `                                ``= ``\$aux_num` `& 1; ` `            ``\$aux_num` `= ``\$aux_num` `>> 1; ` `            ``\$i``++; ` `        ``} ` `    ``} ` `    ``else`     `// Insert bit 1 in the middle ` `    ``{ ` `        ``// Length of the final binary representation ` `        ``\$len_f` `= 2 * ``\$group_no` `+ 1; ` ` `  `        ``// Fill first and last bit as 1 ` `        ``\$a``[``\$len_f` `- 1] = ``\$a`` = 1; ` `        ``\$a``[``\$group_no``] = 1; ` ` `  `        ``/* Start filling the a[] from middle, with ` `        ``the aux_num binary representation */` `        ``while` `(``\$aux_num``) ` `        ``{ ` `            ``// Get the auxiliary number's ith bit and fill ` `            ``// around middle ` `            ``\$a``[``\$group_no` `+ 1 + ``\$i``] = ``\$a``[``\$group_no` `- 1 - ``\$i``] ` `                                ``= ``\$aux_num` `& 1; ` `            ``\$aux_num` `= ``\$aux_num` `>> 1; ` `            ``\$i``++; ` `        ``} ` `    ``} ` ` `  `    ``/* Convert the number to decimal from binary */` `    ``for` `(``\$i` `= 0; ``\$i` `< ``\$len_f``; ``\$i``++) ` `        ``\$num` `+= (1 << ``\$i``) * ``\$a``[``\$i``]; ` `    ``return` `\$num``; ` `} ` ` `  `/* Will return the nth binary palindrome number */` `function` `getNthNumber(``\$n``) ` `{ ` `    ``\$group_no` `= 0; ` `    ``\$count_upto_group` `= 0; ` `    ``\$count_temp` `= 1; ` `    ``\$op``=``\$aux_num``=0; ` ` `  `    ``/* Add number of elements in all the groups, ` `    ``until the group of the nth number is found */` `    ``while` `(``\$count_temp` `< ``\$n``) ` `    ``{ ` `        ``\$group_no``++; ` ` `  `        ``// Total number of elements until this group ` `        ``\$count_upto_group` `= ``\$count_temp``; ` `        ``\$count_temp` `+= 3 * (1 << (``\$group_no` `- 1)); ` `    ``} ` ` `  `    ``// Element's offset position in the group ` `    ``\$group_offset` `= ``\$n` `- ``\$count_upto_group` `- 1; ` ` `  `    ``/* Finding which bit to be placed in the ` `    ``middle and finding the number, which we ` `    ``will fill from the middle in both ` `    ``directions */` `    ``if` `((``\$group_offset` `+ 1) <= (1 << (``\$group_no` `- 1))) ` `    ``{ ` `        ``\$op` `= 2; ``// No need to put extra bit in middle ` ` `  `        ``// We need to fill this auxiliary number ` `        ``// in binary form the middle in both directions ` `        ``\$aux_num` `= ``\$group_offset``; ` `    ``} ` `    ``else` `    ``{ ` `        ``if` `(((``\$group_offset` `+ 1) - (1 << (``\$group_no` `- 1))) % 2) ` `            ``\$op` `= 0; ``// Need to Insert 0 at middle ` `        ``else` `            ``\$op` `= 1; ``// Need to Insert 1 at middle ` `        ``\$aux_num` `= (int)(((``\$group_offset``) - (1 << (``\$group_no` `- 1))) / 2); ` `    ``} ` ` `  `    ``return` `constructNthNumber(``\$group_no``, ``\$aux_num``, ``\$op``); ` `} ` ` `  `    ``// Driver code ` `    ``\$n` `= 9; ` `    ``print``(getNthNumber(``\$n``)); ` ` `  `// This code is contributed by mits ` `?> `

Output :

```27
```

Time complexity of this solution is O(1).

This article is contributed by Kamesh Relangi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.