Skip to content

Practice K-th Symbol in Grammar

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#779
LeetCode Problem

K-th Symbol in Grammar

Problem Statement

We build a table of n rows (1-indexed). We start by writing 0 in the 1st row. Now in every subsequent row, we look at the previous row and replace each occurrence of 0 with 01, and each occurrence of 1 with 10. Given two integer n and k, return the kth (1-indexed) symbol in the nth row of a table of n rows.

Source: K-th Symbol in Grammar on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
n = 1, k = 1
Output
0

# Example 2

Input
n = 2, k = 1
Output
0

# Example 3

Input
n = 2, k = 2
Output
1

Constraints

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

Related LeetCode Problems

Practice more problems similar to K-th Symbol in Grammar.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.