2026-03-03 20:16:05 +0300 MSK
Find Kth Bit in Nth Binary String
Links
Code
class Solution:
def findKthBit(self, n: int, k: int) -> str:
# Find the position of the rightmost set bit in k
# This helps determine which "section" of the string we're in
position_in_section = k & -k
# Determine if k is in the inverted part of the string
# This checks if the bit to the left of the rightmost set bit is 1
is_in_inverted_part = ((k // position_in_section) >> 1 & 1) == 1
# Determine if the original bit (before any inversion) would be 1
# This is true if k is even (i.e., its least significant bit is 0)
original_bit_is_one = (k & 1) == 0
if is_in_inverted_part:
# If we're in the inverted part, we need to flip the bit
return "0" if original_bit_is_one else "1"
else:
# If we're not in the inverted part, return the original bit
return "1" if original_bit_is_one else "0"