![]() |
VOOZH | about |
Given a decimal number m. Consider its binary representation string and apply n iterations. In each iteration, replace the character 0 with the string 01, and 1 with 10. Find the kth (1-based indexing) character in the string after the nth iteration
Examples:
Input: m = 5, n = 2, k = 5
Output: 0
Explanation: Binary representation of m is "101", after one iteration binary representation will be "100110", and after second iteration binary representation will be "100101101001".Input: m = 5, n = 2, k = 1
Output: 1
Explanation: Binary representation of m is "101", after one iteration binary representation will be "100110", and after second iteration binary representation will be "100101101001".
The idea is to do as told in the problem, at every iteration we update the binary representation(i.e. update 0 to 01 and 1 to 10).
Step-By-Step Implementation:
1
The idea is to avoid generating the full binary string after each iteration, since its size doubles every time. Instead, we identify the original bit in m that gives rise to the k-th character after n iterations. The key observation is that the number of bit flips in a path from root to k-th character is equal to the number of set bits in offset, and parity of this count decides the final bit.
The first step will be to find which block the k-th character will be after n iterations are performed. In the n'th iteration distance between any two consecutive characters initially will always be equal to 2^n.
Consider input number m = 5
0th iteration: 101, distance = 0
1st iteration: 10 01 10, distance = 2
2nd iteration: 1001 0110 1001, distance = 4
3rd iteration: 10010110 01101001 10010110, distance = 8
We consider every bit of the input number as the start of a block and aim to determine the block number where the k-th bit will be located after n iterations. T
Finding the Block & Offset Within Block
n iterations, the bits are spaced by 2ⁿ.k = 5 and n = 3, then k % 2nFinding k'th Bit
This offset can be thought of as a path in a binary tree formed by the repeated expansions. Each bit in the binary representation of this offset represents a decision point, a 1 indicates a flip in the bit. So, we count the number of 1s in the offset; this count gives us the number of bit flips applied to the base bit to reach the k-th position.
Finally, we use the parity of this flip count:
b is 0, we start with '0'. An even number of flips keeps it '0', while an odd number of flips changes it to '1'.b is 1, we start with '1'. An even number of flips keeps it '1', while an odd number of flips changes it to '0'.This flipping behavior is directly derived from the rules of expansion (0 → 01, 1 → 10), where each level toggles the bit depending on the path taken. Using this approach, we determine the final k-th bit efficiently in logarithmic time without constructing the full expanded string.
0
The idea is to avoid generating the full binary string after each iteration, since its size doubles every time. Instead, we identify the original bit in m that gives rise to the k-th character after n iterations. The key observation is that the number of bit flips in a path from root to k-th character is equal to the number of set bits in offset, and parity of this count decides the final bit.
0