Problem
Given two positive integers n
and k
, the binary string Sn
is formed as follows:
S1 = "0"
Si = Si - 1 + "1" + reverse(invert(Si - 1))
fori > 1
Where +
denotes the concatenation operation, reverse(x)
returns the reversed string x
, and invert(x)
inverts all the bits in x
(0
changes to 1
and 1
changes to 0
).
For example, the first four strings in the above sequence are:
S1 = "0"
S2 = "0**1**1"
S3 = "011**1**001"
S4 = "0111001**1**0110001"
Return the kth
bit in Sn
. It is guaranteed that k
is valid for the given n
.
Examples
Example 1:
|
|
Example 2:
|
|
Solution
Method 1 - Using recursion
Given the nature of how the binary strings S_n
are generated recursively, explicitly constructing the strings up to S_n
would be inefficient for larger values of n
.
Each binary string is formed recursively by inserting a “1” in the middle and appending an inverted and reversed version of the previous string. So, by understanding the structure and recursive generation of the strings, we can determine the k-th
bit efficiently using recursion or iteration. The recursive relationship allows us to bypass the need to generate the entire string and directly find the k-th bit by:
- Identifying the midpoint.
- Determining the position of
k
relative to the midpoint. - Using the properties of inversion and reversal.
Approach
Here is the approach:
- Recursive Structure:
- Each string
S_n
is derived from the previous stringS_{n-1}
by appending1
and the reversed inverted version ofS_{n-1}
. - Key observations:
- The midpoint of
S_n
is at index2^(n-1)
. - The structure of each part is symmetric.
- The midpoint of
- Each string
- Base Case:
- If
n = 1
, the string is"0"
.
- If
- Recursion:
- If
k
equals the midpoint, return1
. - If
k
lies in the first half, recursively find the bit inS_{n-1}
. - If
k
lies in the second half, find the corresponding position in the first half ofS_{n-1}
, compute the bit, and then invert it.
- If
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n)
, because each recursive call reduces the size by half. - 🧺 Space complexity:
O(n)
, due to recursion stack.