The Indian Engineer

Problem 1545 Find Kth Bit in Nth Binary String

Posted on 5 mins

String Recursion Simulation

Problem Statement

Link - Problem 1545

Question

Given two positive integers n and k, the binary string Sn is formed as follows:

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:

Return the kth bit in Sn. It is guaranteed that k is valid for the given n.

Example 1:

Input: n = 3, k = 1
Output: "0"
Explanation: S3 is "0111001".
The 1st bit is "0".

Example 2:

Input: n = 4, k = 11
Output: "1"
Explanation: S4 is "011100110110001".
The 11th bit is "1".

Constraints:

Solution

class Solution {
public:
    char findKthBit(int n, int k) {
        int invertCount = 0;
        int len = (1 << n) - 1;

        while (k > 1) {
            if (k == len / 2 + 1) {
                return invertCount % 2 == 0 ? '1' : '0';
            }

            if (k > len / 2) {
                k = len + 1 - k;
                invertCount++;
            }

            len /= 2;
        }
        return invertCount % 2 == 0 ? '0' : '1';
    }
};

Complexity Analysis

| Algorithm              | Time Complexity | Space Complexity |
| ---------------------- | --------------- | ---------------- |
| Recursive Construction | O(log n)        | O(1)             |

Explanation

Intial Thoughts

To approach this problem, we should first understand the pattern in the sequence of binary strings. We notice that each string is formed by concatenating the previous string, a ‘1’, and the reverse of the invert of the previous string. We can use this pattern to find the kth bit in the nth string. Initial steps include identifying the length of the string, locating the position of the kth bit, and determining whether the bit is ‘0’ or ‘1’. We need to consider the effect of inverting and reversing on the bits and how it impacts the kth bit. We also need to consider using a while loop to repeatedly divide the length of the string and adjust the position of the kth bit until we can determine its value.

Intuitive Analysis

Intuitively, we can analyze the problem by understanding the properties of the sequence of binary strings. We recognize that the sequence is formed by iterative concatenation and transformation of the previous string. The key insight is to notice that when k is at the midpoint of the current string length, we can determine the bit based on the inversion count. If k is in the second half of the string, we can reflect it to the first half and increment the inversion count. By repeatedly applying this process, we can narrow down the search space and eventually find the kth bit. This approach allows us to avoid constructing the entire string and instead focus on the properties of the sequence to find the desired bit.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Medium difficulty.

Hints

Since n is small, we can simply simulate the process of constructing S1 to Sn.