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.
Input: N = 4, M = 3
Therefore, the 3rd bit in S4 is ‘0’
Input: N = 5, M = 2
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:
Time Complexity: O(N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Maximum Length of Sequence of Sums of prime factors generated by the given operations
- Ratio of mth and nth terms of an A. P. with given ratio of sums
- Sum of P terms of an AP if Mth and Nth terms are given
- Find Pth term of a GP if Mth and Nth terms are given
- Ratio of mth and nth term in an Arithmetic Progression (AP)
- Nth term of a recurrence relation generated by two given arrays
- String generated by typing given string in a keyboard having the button of given character faulty
- Count of 1-bit and 2-bit characters in the given binary string
- Complete the sequence generated by a polynomial
- Check whether given string can be generated after concatenating given strings
- Subsequences generated by including characters or ASCII value of characters of given string
- Find the Mth element of the Array after K left rotations
- Mth element after K Right Rotations of an Array
- Count rectangles generated in a given rectangle by lines drawn parallel to X and Y axis from a given set of points
- Count sequences of given length having non-negative prefix sums that can be generated by given values
- Kth character from the Nth string obtained by the given operations
- Find the element in the matrix generated by given rules
- Find the sum of elements of the Matrix generated by the given rules
- Maximize sum of pairwise products generated from the given Arrays
- Minimum possible Bitwise OR of all Bitwise AND of pairs generated from two given arrays
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.