Find the -th bit in a recursively defined binary string of length .
Find K-th Bit in N-th Binary String
gfgProblem
You are given two integers and .
Define a binary string recursively as follows:
- For , construct by taking , appending a
0, then appending the reverse of with every bit flipped (0becomes1, and1becomes0).
Your task is to return the -th character of using 1-based indexing.
This problem is designed to test your ability to reason about recursive structure without explicitly building the full string.
Input Format
- Two integers and .
- is the recursion level.
- is the 1-based position to query in .
Output Format
- Return the character at position in .
- The answer is either
0or1.
Constraints
- The intended solution should avoid constructing the full string for large .
Example 1
Input
n = 3, k = 1
Output
0
Explanation
, so the first bit is 0.
Example 2
Input
n = 3, k = 5
Output
0
Explanation
, and the 5th bit is the middle bit 0.
Show 1 more example
Example 3
Input
n = 4, k = 11
Output
1
Explanation
The recursive string has length $15$. Tracing the position through mirrored halves gives bit 1 at index 11.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.