|
| 1 | +--- |
| 2 | +id: find-kth-bit-in-nth-binary-string |
| 3 | +title: Find Kth Bit in Nth Binary String |
| 4 | +sidebar_label: 1545 - Find Kth Bit in Nth Binary String |
| 5 | +tags: [Binary String, Kth Bit, Inversion, C++] |
| 6 | +description: Given two positive integers n and k, return the kth bit in the nth binary string generated by specific rules. |
| 7 | +--- |
| 8 | + |
| 9 | +## Find Kth Bit in Nth Binary String |
| 10 | + |
| 11 | +### Problem Statement |
| 12 | +Given two positive integers `n` and `k`, the binary string `S_n` is formed as follows: |
| 13 | + |
| 14 | +- `S1 = "0"` |
| 15 | +- `S_i = S_(i - 1) + "1" + reverse(invert(S_(i - 1)))` for `i > 1` |
| 16 | + |
| 17 | +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). |
| 18 | + |
| 19 | +For example, the first four strings in the above sequence are: |
| 20 | + |
| 21 | +- `S1 = "0"` |
| 22 | +- `S2 = "011"` |
| 23 | +- `S3 = "0111001"` |
| 24 | +- `S4 = "011100110110001"` |
| 25 | + |
| 26 | +Return the `k`th bit in `S_n`. It is guaranteed that `k` is valid for the given `n`. |
| 27 | + |
| 28 | +### Example 1: |
| 29 | +**Input:** `n = 3, k = 1` |
| 30 | +**Output:** `"0"` |
| 31 | +**Explanation:** `S3` is `"0111001"`. The 1st bit is `"0"`. |
| 32 | + |
| 33 | +### Example 2: |
| 34 | +**Input:** `n = 4, k = 11` |
| 35 | +**Output:** `"1"` |
| 36 | +**Explanation:** `S4` is `"011100110110001"`. The 11th bit is `"1"`. |
| 37 | + |
| 38 | +### Constraints |
| 39 | + |
| 40 | +- `1 <= n <= 20` |
| 41 | +- `1 <= k <= 2^n - 1` |
| 42 | +### Intuition |
| 43 | + |
| 44 | +The goal of the solution is to find the `k`th bit in the `n`th binary string `S_n` generated by specific rules. Here's the step-by-step intuition behind the algorithm: |
| 45 | + |
| 46 | +1. **Base Case**: The base case `S1` is initialized as "0". |
| 47 | +2. **Building Subsequent Strings**: For each subsequent `S_i` where `i > 1`, the string is constructed by concatenating `S_(i-1)`, "1", and the inverted and reversed `S_(i-1)`. |
| 48 | +3. **String Inversion and Reversal**: For constructing the inverted and reversed part, iterate through `S_(i-1)` from end to start, inverting each bit (0 becomes 1 and 1 becomes 0). |
| 49 | +4. **Store Intermediate Strings**: Use a hash map (`mp`) to store each `S_i` to avoid recomputation and facilitate quick access. |
| 50 | +5. **Access the k-th Bit**: After constructing `S_n`, access the `k-1` index of the string to get the `k`th bit. |
| 51 | + |
| 52 | +### Time Complexity |
| 53 | + |
| 54 | +The time complexity of the algorithm is $O(2^n)$, where $n$ is the input integer: |
| 55 | + |
| 56 | +- **Constructing Strings**: Each `S_i` is twice the length of `S_(i-1)`, leading to exponential growth. The length of `S_n` is $2^n - 1$. |
| 57 | +- **Inversion and Reversal**: Inverting and reversing the previous string takes linear time with respect to its length, leading to exponential time overall due to the doubling size at each step. |
| 58 | + |
| 59 | +### Space Complexity |
| 60 | + |
| 61 | +The space complexity of the algorithm is $O(2^n)$ in the worst case: |
| 62 | + |
| 63 | +- **Storage in Hash Map**: The hash map stores each intermediate string `S_i`. The total storage needed is the sum of the lengths of all strings up to `S_n`, which is dominated by the length of `S_n`. |
| 64 | +- **Final String Storage**: The final string `S_n` has length $2^n - 1$, contributing to the space complexity. |
| 65 | + |
| 66 | +Overall, both the time and space complexity are exponential in `n`. |
| 67 | + |
| 68 | +### Code |
| 69 | +#### C++ |
| 70 | +```cpp |
| 71 | +class Solution { |
| 72 | +public: |
| 73 | + char findKthBit(int n, int k) { |
| 74 | + |
| 75 | + unordered_map<int,string>mp; |
| 76 | + mp[1]="0"; |
| 77 | + for(int i=2;i<=n;i++) |
| 78 | + { |
| 79 | + |
| 80 | + int j=mp[i-1].length()-1; |
| 81 | + string s1=""; |
| 82 | + while(j>=0){ |
| 83 | + if(mp[i-1][j--]=='0') |
| 84 | + s1+='1'; |
| 85 | + else s1+='0'; |
| 86 | + } |
| 87 | + |
| 88 | + mp[i]+=mp[i-1]+"1"+s1; |
| 89 | + } |
| 90 | + |
| 91 | + string ans=mp[n]; |
| 92 | + return ans[k-1]; |
| 93 | + } |
| 94 | +}; |
| 95 | +``` |
0 commit comments