Problem Description
We build a table of n rows (1-indexed) starting with a single "0" in row 1. For each subsequent row, every occurrence of 0 is replaced with "01" and every occurrence of 1 with "10". Given integers n and k, return the kth (1-indexed) symbol in the nth row.
Key Insights
- The kth symbol in the nth row is derived from a parent symbol in the previous row.
- The parent’s position is determined by (k+1) // 2.
- If k is odd, the kth symbol is identical to its parent; if even, the symbol is flipped.
- Recursion helps trace back to the base case (row 1, symbol "0").
Space and Time Complexity
Time Complexity: O(n) - each recursive call does O(1) work with a maximum recursion depth of n. Space Complexity: O(n) - due to the recursion stack.
Solution
We use a recursive approach where for any kth symbol in the nth row, the parent symbol at row (n-1) is determined using the index (k+1)//2. If k is odd, the symbol remains the same as the parent's; if even, it is flipped (0 becomes 1 and vice versa). This process continues until the base case is reached at n = 1 where the symbol is always 0.