Mth bit in Nth binary string from a sequence generated by the given operations
Last Updated :
24 Mar, 2021
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++
// C++ program for above approach
#include <bits/stdc++.h>
using namespace std;
#define maxN 10
// Function to calculate N
// Fibonacci numbers
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];
}
}
// Function to find the mth bit
// in the string Sn
int find_mth_bit(int n, int m, int fib[])
{
// Base case
if (n <= 1) {
return n;
}
// Length of left half
int len_left = fib[n - 2];
// Length of the right half
int len_right = fib[n - 1];
if (m <= len_left) {
// Recursive check in the left half
return find_mth_bit(n - 2,
len_left + 1 - m, fib);
}
else {
// Recursive check in the right half
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 << ' ';
}
// Driver Code
int main()
{
int n = 5, m = 3;
find_mth_bitUtil(n, m);
return 0;
}
Java
// Java program for
// the above approach
import java.util.*;
class GFG{
static final int maxN = 10;
// Function to calculate N
// Fibonacci numbers
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];
}
}
// Function to find the mth bit
// in the String Sn
static int find_mth_bit(int n,
int m,
int fib[])
{
// Base case
if (n <= 1)
{
return n;
}
// Length of left half
int len_left = fib[n - 2];
// Length of the right half
int len_right = fib[n - 1];
if (m <= len_left)
{
// Recursive check in
// the left half
return find_mth_bit(n - 2,
len_left +
1 - m, fib);
}
else
{
// Recursive check in
// the right half
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 + " ");
}
// Driver Code
public static void main(String[] args)
{
int n = 5, m = 3;
find_mth_bitUtil(n, m);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program for above approach
maxN = 10
# Function to calculate N
# Fibonacci numbers
def calculateFib(fib, n):
fib[0] = fib[1] = 1
for x in range(2, n):
fib[x] = (fib[x - 1] +
fib[x - 2])
# Function to find the mth bit
# in the string Sn
def find_mth_bit(n, m, fib):
# Base case
if (n <= 1):
return n
# Length of left half
len_left = fib[n - 2]
# Length of the right half
len_right = fib[n - 1]
if (m <= len_left):
# Recursive check in the left half
return find_mth_bit(n - 2,
len_left + 1 - m, fib)
else:
# Recursive check in the right half
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)
# Driver Code
if __name__ == '__main__':
n = 5
m = 3
find_mth_bitUtil(n, m)
# This code is contributed by mohit kumar 29
C#
// C# program for
// the above approach
using System;
class GFG{
static int maxN = 10;
// Function to calculate N
// Fibonacci numbers
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];
}
}
// Function to find the mth bit
// in the String Sn
static int find_mth_bit(int n,
int m,
int []fib)
{
// Base case
if (n <= 1)
{
return n;
}
// Length of left half
int len_left = fib[n - 2];
// Length of the right half
int len_right = fib[n - 1];
if (m <= len_left)
{
// Recursive check in
// the left half
return find_mth_bit(n - 2,
len_left +
1 - m, fib);
}
else
{
// Recursive check in
// the right half
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 + " ");
}
// Driver Code
public static void Main()
{
int n = 5, m = 3;
find_mth_bitUtil(n, m);
}
}
// This code is contributed by Chitranayal
JavaScript
<script>
// JavaScript program for above approach
const maxN = 10
// Function to calculate N
// Fibonacci numbers
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 to find the mth bit
// in the string Sn
function find_mth_bit(n, m, fib)
{
// Base case
if (n <= 1) {
return n;
}
// Length of left half
let len_left = fib[n - 2];
// Length of the right half
let len_right = fib[n - 1];
if (m <= len_left) {
// Recursive check in the left half
return find_mth_bit(n - 2,
len_left + 1 - m, fib);
}
else {
// Recursive check in the right half
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 + " ");
}
// Driver Code
let n = 5, m = 3;
find_mth_bitUtil(n, m);
// This code is contributed by Surbhi Tyagi.
</script>
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Find k-th bit in a binary string created by repeated invert and append operations You are given an initial string s starting with "0". The string keeps duplicating as follows. Invert of it is appended to it.Examples: Input : k = 2 Output : 1 Initially s = "0". First Iteration : s = s + s' = "01" Second Iteration : s = s + s' = "0110" The digit at index 2 of s is 1. Input : k = 12
8 min read
Generate all binary strings from given pattern Given a string containing of '0', '1' and '?' wildcard characters, generate all binary strings that can be formed by replacing each wildcard character by '0' or '1'. Example : Input: str = "1??0?101"Output: 1000010110001101101001011010110111000101110011011110010111101101 Recommended PracticeGenerate
13 min read
Minimum given operations required to convert a given binary string to all 1's Given a binary number as a string str of length L. The task is to find the minimum number of operations needed so that the number becomes 2L-1, that is a string consisting of only 1's of the length L. In each operation, the number N can be replaced by N xor (N + 1). Examples: Input: str = "10010111"
7 min read
Minimum number of operations required to obtain a given Binary String Given a binary strings S of length N, the task is to obtain S from a string, say T, of length N consisting only of zeroes, by minimum number of operations. Each operation involves choosing any index i from string S and flipping all the bits at indices [i, N - 1] of the string T. Examples: Input: S =
8 min read
Position of leftmost set bit in given binary string where all 1s appear at end Given a binary string S of length N, such that all 1s appear on the right. The task is to return the index of the first set bit found from the left side else return -1. Examples: Input: s = 00011, N = 5Output: 3Explanation: The first set bit from the left side is at index 3. Input: s = 0000, N = 4Ou
5 min read
Generate all the binary strings of N bits Given a positive integer number N. The task is to generate all the binary strings of N bits. These binary strings should be in ascending order.Examples: Input: 2Output:0 00 11 01 1Input: 3Output:0 0 00 0 10 1 00 1 11 0 01 0 11 1 01 1 1Approach: The idea is to try every permutation. For every positio
8 min read