Find Kth Bit in Nth Binary String
Medium Recursion AI round
A sequence of binary strings is made round by round. The first string, S1, is "0". Each string after it is made from the one before: write the previous string, then a single "1", then the previous string again with every bit flipped (each 0 becomes a 1, and each 1 a 0) and read from the last character to the first.
Given n and k, look at the string Sn, and find its k-th character, counting from 1.
Return that character as a string of one character, "0" or "1".
Example 1
S1 is "0". S2 is "0", then "1", then "0" flipped and reversed, "1": "011". S3 is "011", then "1", then "011" flipped, "100", and reversed, "001": "0111001", and its 5th character is 0.
Example 2
S4 is 011100110110001. S5 is S4, then a 1, then S4 flipped, 100011001001110, and read backwards, 011100100110001: 0111001101100011011100100110001. Its 27th character is 1.
Example 3
Constraints
1 ≤ n ≤ 311 ≤ k ≤ 2ⁿ - 1, the length ofSn, soknever passes2³¹ - 1and fits in a signed 32-bit integer
Practice this in an AI round
30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.
Your first AI round is free.
More Recursion
- Permutation SequenceAI round · Hard
- Count Good NumbersAI round · Medium
- Pow(x, n)Medium
- Power of ThreeEasy
- Elimination GameMedium