Mth bit in Nth binary string from a sequence generated by the given operations
Given two integers N and M, generate a sequence of N binary strings by the following steps:
- S0 = “0”
- S1 = “1”
- Generate remaining strings by the equation Si = reverse(Si – 2) + reverse(Si – 1)
The task is to find the Mth set bit in the Nth string.
Examples:
Input: N = 4, M = 3
Output: 0
Explanation:
S0 =”0″
S1 =”1″
S2 =”01″
S3 =”110″
S4 =”10011″
Therefore, the 3rd bit in S4 is ‘0’
Input: N = 5, M = 2
Output: 1
Naive Approach: The simplest approach is to generate S2 to SN – 1 and traverse the string SN – 1 to find the Mth bit.
Time Complexity: O(N * 2N)
Auxiliary Space: O(N)
Efficient Approach: Follow the steps below to optimize the above approach:
- Compute and store the first N Fibonacci numbers in an array, say fib[]
- Now, search for the Mth bit in the Nth string.
- If N > 1 : Considering SN to be the concatenation of reverse of string SN – 2 and reverse of string SN – 1, the length of the string SN – 2 is equal to fib[N – 2] and length of the string SN – 1 is equal to fib[N – 1].
- If M ? fib[n-2]: It signifies that M lies in SN – 2, therefore, recursively search for the (fib[N – 2] + 1 – M)th bit of the string SN – 2.
- If M > fib[N – 2]: It signifies that M lies in SN – 1, therefore, recursively search for the (fib[N – 1]+ 1 – (M – fib[N – 2]))th bit of SN – 1.
- If N ? 1: return N.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define maxN 10
void calculateFib( int fib[], int n)
{
fib[0] = fib[1] = 1;
for ( int x = 2; x < n; x++) {
fib[x] = fib[x - 1] + fib[x - 2];
}
}
int find_mth_bit( int n, int m, int fib[])
{
if (n <= 1) {
return n;
}
int len_left = fib[n - 2];
int len_right = fib[n - 1];
if (m <= len_left) {
return find_mth_bit(n - 2,
len_left + 1 - m, fib);
}
else {
return find_mth_bit(
n - 1, len_right + 1
- (m - len_left),
fib);
}
}
void find_mth_bitUtil( int n, int m)
{
int fib[maxN];
calculateFib(fib, maxN);
int ans = find_mth_bit(n, m, fib);
cout << ans << ' ' ;
}
int main()
{
int n = 5, m = 3;
find_mth_bitUtil(n, m);
return 0;
}
|
Java
import java.util.*;
class GFG{
static final int maxN = 10 ;
static void calculateFib( int fib[],
int n)
{
fib[ 0 ] = fib[ 1 ] = 1 ;
for ( int x = 2 ; x < n; x++)
{
fib[x] = fib[x - 1 ] +
fib[x - 2 ];
}
}
static int find_mth_bit( int n,
int m,
int fib[])
{
if (n <= 1 )
{
return n;
}
int len_left = fib[n - 2 ];
int len_right = fib[n - 1 ];
if (m <= len_left)
{
return find_mth_bit(n - 2 ,
len_left +
1 - m, fib);
}
else
{
return find_mth_bit(n - 1 ,
len_right +
1 - (m -
len_left), fib);
}
}
static void find_mth_bitUtil( int n, int m)
{
int []fib = new int [maxN];
calculateFib(fib, maxN);
int ans = find_mth_bit(n, m, fib);
System.out.print(ans + " " );
}
public static void main(String[] args)
{
int n = 5 , m = 3 ;
find_mth_bitUtil(n, m);
}
}
|
Python3
maxN = 10
def calculateFib(fib, n):
fib[ 0 ] = fib[ 1 ] = 1
for x in range ( 2 , n):
fib[x] = (fib[x - 1 ] +
fib[x - 2 ])
def find_mth_bit(n, m, fib):
if (n < = 1 ):
return n
len_left = fib[n - 2 ]
len_right = fib[n - 1 ]
if (m < = len_left):
return find_mth_bit(n - 2 ,
len_left + 1 - m, fib)
else :
return find_mth_bit(n - 1 ,
len_right + 1 -
(m - len_left), fib)
def find_mth_bitUtil(n, m):
fib = [ 0 for i in range (maxN)]
calculateFib(fib, maxN)
ans = find_mth_bit(n, m, fib)
print (ans)
if __name__ = = '__main__' :
n = 5
m = 3
find_mth_bitUtil(n, m)
|
C#
using System;
class GFG{
static int maxN = 10;
static void calculateFib( int []fib ,
int n)
{
fib[0] = fib[1] = 1;
for ( int x = 2; x < n; x++)
{
fib[x] = fib[x - 1] +
fib[x - 2];
}
}
static int find_mth_bit( int n,
int m,
int []fib)
{
if (n <= 1)
{
return n;
}
int len_left = fib[n - 2];
int len_right = fib[n - 1];
if (m <= len_left)
{
return find_mth_bit(n - 2,
len_left +
1 - m, fib);
}
else
{
return find_mth_bit(n - 1,
len_right +
1 - (m -
len_left), fib);
}
}
static void find_mth_bitUtil( int n,
int m)
{
int []fib = new int [maxN];
calculateFib(fib, maxN);
int ans = find_mth_bit(n, m, fib);
Console.Write(ans + " " );
}
public static void Main()
{
int n = 5, m = 3;
find_mth_bitUtil(n, m);
}
}
|
Javascript
<script>
const maxN = 10
function calculateFib(fib, n)
{
fib[0] = fib[1] = 1;
for (let x = 2; x < n; x++) {
fib[x] = fib[x - 1] + fib[x - 2];
}
}
function find_mth_bit(n, m, fib)
{
if (n <= 1) {
return n;
}
let len_left = fib[n - 2];
let len_right = fib[n - 1];
if (m <= len_left) {
return find_mth_bit(n - 2,
len_left + 1 - m, fib);
}
else {
return find_mth_bit(
n - 1, len_right + 1
- (m - len_left),
fib);
}
}
function find_mth_bitUtil(n, m)
{
let fib = new Array(maxN);
calculateFib(fib, maxN);
let ans = find_mth_bit(n, m, fib);
document.write(ans + " " );
}
let n = 5, m = 3;
find_mth_bitUtil(n, m);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
24 Mar, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...