K-th Symbol in Grammar

Medium Topics Company Tags

The first row of a table is 0. To produce the next row, replace every 0 with 01 and every 1 with 10, keeping their order. Return the symbol at position k in row n. Both rows and positions are numbered starting at 1.

Example 1:

Input: n = 3
k = 3

Output: 1

Explanation: Row 3 is 0110, whose third symbol is 1.

Example 2:

Input: n = 4
k = 5

Output: 1

Explanation: Row 4 is 01101001, so its fifth symbol is 1.

Example 3:

Input: n = 4
k = 8

Output: 1

Explanation: The eighth and final symbol of row 4, 01101001, is 1.

Constraints:

  • 1 <= n <= 30
  • 1 <= k <= 2^(n - 1)


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.