Find the n-th number whose binary representation is a palindrome
TFind 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
import java.io.*;
class GFG {
static int INT_MAX = 2147483647 ;
static int isKthBitSet( int x, int k)
{
return ((x & ( 1 << (k - 1 ))) > 0 ) ? 1 : 0 ;
}
static int leftmostSetBit( int x)
{
int count = 0 ;
while (x > 0 ) {
count++;
x = x >> 1 ;
}
return count;
}
static int isBinPalindrome( int x)
{
int l = leftmostSetBit(x);
int r = 1 ;
while (l > r) {
if (isKthBitSet(x, l) != isKthBitSet(x, r))
return 0 ;
l--;
r++;
}
return 1 ;
}
static int findNthPalindrome( int n)
{
int pal_count = 0 ;
int i = 0 ;
for (i = 1 ; i <= INT_MAX; i++) {
if (isBinPalindrome(i) > 0 ) {
pal_count++;
}
if (pal_count == n)
break ;
}
return i;
}
public static void main(String[] args)
{
int n = 9 ;
System.out.println(findNthPalindrome(n));
}
}
|
Python3
C#
PHP
<?php
function isKthBitSet( $x , $k )
{
return ( $x & (1 << ( $k - 1))) ? 1 : 0;
}
function leftmostSetBit( $x )
{
$count = 0;
while ( $x )
{
$count ++;
$x = $x >> 1;
}
return $count ;
}
function isBinPalindrome( $x )
{
$l = leftmostSetBit( $x );
$r = 1;
while ( $l > $r )
{
if (isKthBitSet( $x , $l ) !=
isKthBitSet( $x , $r ))
return 0;
$l --;
$r ++;
}
return 1;
}
function findNthPalindrome( $n )
{
$pal_count = 0;
$i = 0;
for ( $i = 1; $i <= PHP_INT_MAX; $i ++)
{
if (isBinPalindrome( $i ))
{
$pal_count ++;
}
if ( $pal_count == $n )
break ;
}
return $i ;
}
$n = 9;
echo (findNthPalindrome( $n ));
?>
|
Javascript
<script>
let INT_MAX = 2147483647;
function isKthBitSet(x, k)
{
return ((x & (1 << (k - 1))) > 0) ? 1 : 0;
}
function leftmostSetBit(x)
{
let count = 0;
while (x > 0)
{
count++;
x = x >> 1;
}
return count;
}
function isBinPalindrome(x)
{
let l = leftmostSetBit(x);
let r = 1;
while (l > r)
{
if (isKthBitSet(x, l) !=
isKthBitSet(x, r))
return 0;
l--;
r++;
}
return 1;
}
function findNthPalindrome(n)
{
let pal_count = 0;
let i = 0;
for (i = 1; i <= INT_MAX; i++)
{
if (isBinPalindrome(i) > 0)
{
pal_count++;
}
if (pal_count == n)
break ;
}
return i;
}
let n = 9;
document.write(findNthPalindrome(n));
</script>
|
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
- if the curr string of even length then add “0” and “1” at the mid of curr string and add it into the queue.
- 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 odd length
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int convertStringToInt(string s)
{
int num = 0;
for ( int i = 0; i < s.size(); i++) {
num = num * 2 + (s[i] - '0' );
}
return num;
}
int getNthNumber( int n)
{
if (n == 1)
return 1;
n--;
queue<string> q;
q.push( "11" );
while (!q.empty()) {
string curr = q.front();
q.pop();
n--;
if (!n) {
return convertStringToInt(curr);
}
int mid = curr.size() / 2;
if (curr.size() % 2 == 0) {
string s0 = curr, s1 = curr;
s0.insert(mid, "0" );
s1.insert(mid, "1" );
q.push(s0);
q.push(s1);
}
else {
string ch(1, curr[mid]);
string temp = curr;
temp.insert(mid, ch);
q.push(temp);
}
}
return 0;
}
int main()
{
int n = 9;
cout << getNthNumber(n);
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static int convertStringToInt(String s)
{
int ans = 0 ;
for ( int i = 0 ; i < s.length(); i++) {
if (s.charAt(i) == '1' )
ans += 1 << i;
}
return ans;
}
public static int getNthNumber( int n)
{
Queue<String> q = new LinkedList<>();
if (n == 1 )
return 1 ;
n = n - 1 ;
q.add( "11" );
while (n-- > 0 ) {
String curr = q.remove();
if (n == 0 )
return convertStringToInt(curr);
int len = curr.length();
if (len % 2 == 0 ) {
q.add(curr.substring( 0 , len / 2 ) + "0"
+ curr.substring(len / 2 ));
q.add(curr.substring( 0 , len / 2 ) + "1"
+ curr.substring(len / 2 ));
}
else {
char midChar = curr.charAt(len / 2 );
q.add(curr.substring( 0 , len / 2 ) + midChar
+ curr.substring(len / 2 ));
}
}
return - 1 ;
}
public static void main(String[] args)
{
int n = 9 ;
System.out.println(getNthNumber(n));
}
}
|
Python3
def convertStringToInt(s):
ans = 0
for i in range ( len (s)):
ans = ans * 2 + ( ord (s[i]) - ord ( '0' ))
return ans
def getNthNumber(n):
q = []
if (n = = 1 ):
return 1
n = n - 1
q.append( "11" )
while ( len (q) ! = 0 ):
curr = q.pop( 0 )
n - = 1
if (n = = 0 ):
return convertStringToInt(curr)
lenn = len (curr)
if ( len (curr) % 2 = = 0 ):
q.append(curr[ 0 : int (lenn / 2 )] + "0" + curr[ int (lenn / 2 ):])
q.append(curr[ 0 : int (lenn / 2 )] + "1" + curr[ int (lenn / 2 ):])
else :
midChar = curr[ int (lenn / 2 )]
q.append(curr[ 0 : int (lenn / 2 )] + midChar + curr[ int (lenn / 2 ):])
return 0
n = 9
print (getNthNumber(n))
|
C#
using System;
using System.Collections.Generic;
class GFG{
public static int convertStringToInt(String s)
{
int ans = 0;
for ( int i = 0; i < s.Length; i++)
{
if (s[i] == '1' )
ans += 1 << i;
}
return ans;
}
public static int getNthNumber( int n)
{
Queue<String> q = new Queue<String>();
if (n == 1)
return 1;
n = n - 1;
q.Enqueue( "11" );
while (n-- > 0)
{
String curr = q.Dequeue();
if (n == 0)
return convertStringToInt(curr);
int len = curr.Length;
if (len % 2 == 0)
{
q.Enqueue(curr.Substring(0, len / 2) + "0" +
curr.Substring(len / 2));
q.Enqueue(curr.Substring(0, len / 2) + "1" +
curr.Substring(len / 2));
}
else
{
char midChar = curr[len / 2];
q.Enqueue(curr.Substring(0, len / 2) + midChar +
curr.Substring(len / 2));
}
}
return -1;
}
public static void Main(String[] args)
{
int n = 9;
Console.WriteLine(getNthNumber(n));
}
}
|
Javascript
<script>
function convertStringToInt(s)
{
let ans = 0;
for (let i = 0; i < s.length; i++) {
if (s[i] == '1' )
ans += 1 << i;
}
return ans;
}
function getNthNumber(n)
{
let q = [];
if (n == 1)
return 1;
n = n - 1;
q.push( "11" );
while (n-- > 0) {
let curr = q.shift();
if (n == 0)
return convertStringToInt(curr);
let len = curr.length;
let len2 = Math.floor(len / 2);
if (len % 2 == 0) {
q.push(curr.substring(0, len2) + "0"
+ curr.substring(len2));
q.push(curr.substring(0, len2) + "1"
+ curr.substring(len2));
}
else {
let midChar = curr[len2];
q.push(curr.substring(0, len2) + midChar
+ curr.substring(len2));
}
}
return -1;
}
let n = 9;
document.write(getNthNumber(n));
</script>
|
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++
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int constructNthNumber( int group_no, int aux_num, int op)
{
int INT_SIZE = 32 ;
int a[INT_SIZE] = { 0 };
int num = 0, len_f;
int i = 0;
if (op == 2)
{
len_f = 2 * group_no;
a[len_f - 1] = a[0] = 1;
while (aux_num)
{
a[group_no + i]
= a[group_no - 1 - i]
= aux_num & 1;
aux_num = aux_num >> 1;
i++;
}
}
else if (op == 0)
{
len_f = 2 * group_no + 1;
a[len_f - 1] = a[0] = 1;
a[group_no] = 0;
while (aux_num)
{
a[group_no + 1 + i]
= a[group_no - 1 - i]
= aux_num & 1;
aux_num = aux_num >> 1;
i++;
}
}
else
{
len_f = 2 * group_no + 1;
a[len_f - 1] = a[0] = 1;
a[group_no] = 1;
while (aux_num)
{
a[group_no + 1 + i]
= a[group_no - 1 - i]
= aux_num & 1;
aux_num = aux_num >> 1;
i++;
}
}
for (i = 0; i < len_f; i++)
num += (1 << i) * a[i];
return num;
}
int getNthNumber( int n)
{
int group_no = 0, group_offset;
int count_upto_group = 0, count_temp = 1;
int op, aux_num;
while (count_temp < n)
{
group_no++;
count_upto_group = count_temp;
count_temp += 3 * (1 << (group_no - 1));
}
group_offset = n - count_upto_group - 1;
if ((group_offset + 1) <= (1 << (group_no - 1)))
{
op = 2;
aux_num = group_offset;
}
else
{
if (((group_offset + 1) -
(1 << (group_no - 1))) % 2)
op = 0;
else
op = 1;
aux_num = ((group_offset) -
(1 << (group_no - 1))) / 2;
}
return constructNthNumber(group_no, aux_num, op);
}
int main()
{
int n = 9;
cout << getNthNumber(n) ;
return 0;
}
|
C
#include <stdio.h>
#define INT_SIZE 32
int constructNthNumber( int group_no, int aux_num, int op)
{
int a[INT_SIZE] = { 0 };
int num = 0, len_f;
int i = 0;
if (op == 2) {
len_f = 2 * group_no;
a[len_f - 1] = a[0] = 1;
while (aux_num) {
a[group_no + i]
= a[group_no - 1 - i]
= aux_num & 1;
aux_num = aux_num >> 1;
i++;
}
}
else if (op == 0) {
len_f = 2 * group_no + 1;
a[len_f - 1] = a[0] = 1;
a[group_no] = 0;
while (aux_num) {
a[group_no + 1 + i]
= a[group_no - 1 - i]
= aux_num & 1;
aux_num = aux_num >> 1;
i++;
}
}
else
{
len_f = 2 * group_no + 1;
a[len_f - 1] = a[0] = 1;
a[group_no] = 1;
while (aux_num) {
a[group_no + 1 + i]
= a[group_no - 1 - i]
= aux_num & 1;
aux_num = aux_num >> 1;
i++;
}
}
for (i = 0; i < len_f; i++)
num += (1 << i) * a[i];
return num;
}
int getNthNumber( int n)
{
int group_no = 0, group_offset;
int count_upto_group = 0, count_temp = 1;
int op, aux_num;
while (count_temp < n) {
group_no++;
count_upto_group = count_temp;
count_temp += 3 * (1 << (group_no - 1));
}
group_offset = n - count_upto_group - 1;
if ((group_offset + 1) <= (1 << (group_no - 1))) {
op = 2;
aux_num = group_offset;
}
else {
if (((group_offset + 1)
- (1 << (group_no - 1))) % 2)
op = 0;
else
op = 1;
aux_num
= ((group_offset) - (1 << (group_no - 1))) / 2;
}
return constructNthNumber(group_no, aux_num, op);
}
int main()
{
int n = 9;
printf ( "%d" , getNthNumber(n));
return 0;
}
|
Java
class GFG {
static int INT_SIZE = 32 ;
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 ;
if (op == 2 ) {
len_f = 2 * group_no;
a[len_f - 1 ] = a[ 0 ] = 1 ;
while (aux_num > 0 ) {
a[group_no + i]
= a[group_no - 1 - i]
= aux_num & 1 ;
aux_num = aux_num >> 1 ;
i++;
}
}
else if (op == 0 ) {
len_f = 2 * group_no + 1 ;
a[len_f - 1 ] = a[ 0 ] = 1 ;
a[group_no] = 0 ;
while (aux_num > 0 ) {
a[group_no + 1 + i]
= a[group_no - 1 - i]
= aux_num & 1 ;
aux_num = aux_num >> 1 ;
i++;
}
}
else
{
len_f = 2 * group_no + 1 ;
a[len_f - 1 ] = a[ 0 ] = 1 ;
a[group_no] = 1 ;
while (aux_num > 0 ) {
a[group_no + 1 + i]
= a[group_no - 1 - i]
= aux_num & 1 ;
aux_num = aux_num >> 1 ;
i++;
}
}
for (i = 0 ; i < len_f; i++)
num += ( 1 << i) * a[i];
return num;
}
static int getNthNumber( int n)
{
int group_no = 0 , group_offset;
int count_upto_group = 0 , count_temp = 1 ;
int op, aux_num;
while (count_temp < n) {
group_no++;
count_upto_group = count_temp;
count_temp += 3 * ( 1 << (group_no - 1 ));
}
group_offset = n - count_upto_group - 1 ;
if ((group_offset + 1 ) <= ( 1 << (group_no - 1 ))) {
op = 2 ;
aux_num = group_offset;
}
else {
if (((group_offset + 1 )
- ( 1 << (group_no - 1 ))) % 2 == 1 )
op = 0 ;
else
op = 1 ;
aux_num
= ((group_offset)
- ( 1 << (group_no - 1 ))) / 2 ;
}
return constructNthNumber(group_no, aux_num, op);
}
public static void main(String[] args)
{
int n = 9 ;
System.out.printf( "%d" , getNthNumber(n));
}
}
|
Python3
INT_SIZE = 32
def constructNthNumber(group_no, aux_num, op):
a = [ 0 ] * INT_SIZE
num, i = 0 , 0
if op = = 2 :
len_f = 2 * group_no
a[len_f - 1 ] = a[ 0 ] = 1
while aux_num:
a[group_no + i] = a[group_no - 1 - i] = \
aux_num & 1
aux_num = aux_num >> 1
i + = 1
elif op = = 0 :
len_f = 2 * group_no + 1
a[len_f - 1 ] = a[ 0 ] = 1
a[group_no] = 0
while aux_num:
a[group_no + 1 + i] = a[group_no - 1 - i] = \
aux_num & 1
aux_num = aux_num >> 1
i + = 1
else :
len_f = 2 * group_no + 1
a[len_f - 1 ] = a[ 0 ] = 1
a[group_no] = 1
while aux_num:
a[group_no + 1 + i] = a[group_no - 1 - i] = \
aux_num & 1
aux_num = aux_num >> 1
i + = 1
for i in range ( 0 , len_f):
num + = ( 1 << i) * a[i]
return num
def getNthNumber(n):
group_no = 0
count_upto_group, count_temp = 0 , 1
while count_temp < n:
group_no + = 1
count_upto_group = count_temp
count_temp + = 3 * ( 1 << (group_no - 1 ))
group_offset = n - count_upto_group - 1
if (group_offset + 1 ) < = ( 1 << (group_no - 1 )):
op = 2
aux_num = group_offset
else :
if (((group_offset + 1 ) -
( 1 << (group_no - 1 ))) % 2 ):
op = 0
else :
op = 1
aux_num = (((group_offset) -
( 1 << (group_no - 1 ))) / / 2 )
return constructNthNumber(group_no, aux_num, op)
if __name__ = = "__main__" :
n = 9
print (getNthNumber(n))
|
C#
using System;
class GFG {
static int INT_SIZE = 32;
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;
if (op == 2) {
len_f = 2 * group_no;
a[len_f - 1] = a[0] = 1;
while (aux_num > 0) {
a[group_no + i] = a[group_no - 1 - i]
= aux_num & 1;
aux_num = aux_num >> 1;
i++;
}
}
else if (op == 0) {
len_f = 2 * group_no + 1;
a[len_f - 1] = a[0] = 1;
a[group_no] = 0;
while (aux_num > 0) {
a[group_no + 1 + i] = a[group_no - 1 - i]
= aux_num & 1;
aux_num = aux_num >> 1;
i++;
}
}
else
{
len_f = 2 * group_no + 1;
a[len_f - 1] = a[0] = 1;
a[group_no] = 1;
while (aux_num > 0) {
a[group_no + 1 + i] = a[group_no - 1 - i]
= aux_num & 1;
aux_num = aux_num >> 1;
i++;
}
}
for (i = 0; i < len_f; i++)
num += (1 << i) * a[i];
return num;
}
static int getNthNumber( int n)
{
int group_no = 0, group_offset;
int count_upto_group = 0, count_temp = 1;
int op, aux_num;
while (count_temp < n) {
group_no++;
count_upto_group = count_temp;
count_temp += 3 * (1 << (group_no - 1));
}
group_offset = n - count_upto_group - 1;
if ((group_offset + 1) <= (1 << (group_no - 1))) {
op = 2;
aux_num = group_offset;
}
else {
if (((group_offset + 1) - (1 << (group_no - 1)))
% 2
== 1)
op = 0;
else
op = 1;
aux_num
= ((group_offset) - (1 << (group_no - 1)))
/ 2;
}
return constructNthNumber(group_no, aux_num, op);
}
public static void Main(String[] args)
{
int n = 9;
Console.Write( "{0}" , getNthNumber(n));
}
}
|
PHP
<?php
$INT_SIZE =32;
function constructNthNumber( $group_no , $aux_num , $op )
{
global $INT_SIZE ;
$a = array_fill (0, $INT_SIZE ,0);
$num = 0;
$i = 0;
$len_f =0;
if ( $op == 2)
{
$len_f = 2 * $group_no ;
$a [ $len_f - 1] = $a [0] = 1;
while ( $aux_num )
{
$a [ $group_no + i] = $a [ $group_no - 1 - $i ]
= $aux_num & 1;
$aux_num = $aux_num >> 1;
$i ++;
}
}
else if ( $op == 0)
{
$len_f = 2 * $group_no + 1;
$a [ $len_f - 1] = $a [0] = 1;
$a [ $group_no ] = 0;
while ( $aux_num )
{
$a [ $group_no + 1 + $i ] = $a [ $group_no - 1 - $i ]
= $aux_num & 1;
$aux_num = $aux_num >> 1;
$i ++;
}
}
else
{
$len_f = 2 * $group_no + 1;
$a [ $len_f - 1] = $a [0] = 1;
$a [ $group_no ] = 1;
while ( $aux_num )
{
$a [ $group_no + 1 + $i ] = $a [ $group_no - 1 - $i ]
= $aux_num & 1;
$aux_num = $aux_num >> 1;
$i ++;
}
}
for ( $i = 0; $i < $len_f ; $i ++)
$num += (1 << $i ) * $a [ $i ];
return $num ;
}
function getNthNumber( $n )
{
$group_no = 0;
$count_upto_group = 0;
$count_temp = 1;
$op = $aux_num =0;
while ( $count_temp < $n )
{
$group_no ++;
$count_upto_group = $count_temp ;
$count_temp += 3 * (1 << ( $group_no - 1));
}
$group_offset = $n - $count_upto_group - 1;
if (( $group_offset + 1) <= (1 << ( $group_no - 1)))
{
$op = 2;
$aux_num = $group_offset ;
}
else
{
if ((( $group_offset + 1) - (1 << ( $group_no - 1))) % 2)
$op = 0;
else
$op = 1;
$aux_num = (int)((( $group_offset ) - (1 << ( $group_no - 1))) / 2);
}
return constructNthNumber( $group_no , $aux_num , $op );
}
$n = 9;
print (getNthNumber( $n ));
?>
|
Javascript
<script>
var INT_SIZE = 32;
function constructNthNumber(group_no , aux_num,op)
{
var a = Array.from({length: INT_SIZE}, (_, i) => 0);
var num = 0, len_f=0;
var i = 0;
if (op == 2)
{
len_f = 2 * group_no;
a[len_f - 1] = a[0] = 1;
while (aux_num > 0)
{
a[group_no + i]
= a[group_no - 1 - i]
= aux_num & 1;
aux_num = aux_num >> 1;
i++;
}
}
else if (op == 0)
{
len_f = 2 * group_no + 1;
a[len_f - 1] = a[0] = 1;
a[group_no] = 0;
while (aux_num > 0)
{
a[group_no + 1 + i]
= a[group_no - 1 - i]
= aux_num & 1;
aux_num = aux_num >> 1;
i++;
}
}
else
{
len_f = 2 * group_no + 1;
a[len_f - 1] = a[0] = 1;
a[group_no] = 1;
while (aux_num > 0)
{
a[group_no + 1 + i]
= a[group_no - 1 - i]
= aux_num & 1;
aux_num = aux_num >> 1;
i++;
}
}
for (i = 0; i < len_f; i++)
num += (1 << i) * a[i];
return num;
}
function getNthNumber(n)
{
var group_no = 0, group_offset;
var count_upto_group = 0, count_temp = 1;
var op, aux_num;
while (count_temp < n)
{
group_no++;
count_upto_group = count_temp;
count_temp += 3 * (1 << (group_no - 1));
}
group_offset = n - count_upto_group - 1;
if ((group_offset + 1) <= (1 << (group_no - 1)))
{
op = 2;
aux_num = group_offset;
}
else
{
if (((group_offset + 1) -
(1 << (group_no - 1))) % 2 == 1)
op = 0;
else
op = 1;
aux_num = ((group_offset) -
(1 << (group_no - 1))) / 2;
}
return constructNthNumber(group_no, aux_num, op);
}
var n = 9;
document.write(getNthNumber(n));
</script>
|
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.
Last Updated :
27 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...