KatInterview

Find Contiguous Prefix Values in Permutation

Arrays & HashingAuto-graded · 21 test cases
MediumAuto-gradedArrays & Hashingarrayspermutationssliding-window

Last updated

Tests understanding of positional reasoning and incremental range tracking in permutations to identify when values 1..k occupy consecutive indices.

You are given a permutation perm consisting of the integers from 1 to n in some order.

For each integer k where 1 ≤ k ≤ n, we say that k is contiguous if the elements with values 1, 2, ..., k appear consecutively somewhere in the permutation (though not necessarily in sorted order within that segment).

More formally, k is contiguous if there exists some starting index i such that the subarray perm[i..i+k-1] contains exactly the values {1, 2, ..., k} as a set.

Return a binary string of length n where the character at position j (0-indexed) is '1' if k = j + 1 is contiguous, and '0' otherwise.

Constraints

  • 1 ≤ n ≤ 200,000
  • perm is a valid permutation of 1 to n
  • Each value from 1 to n appears exactly once in perm

Examples

Example 1

Input: find_contiguous_prefix_values([4, 1, 3, 2, 5])

Output: '10111'

Hints

Hint 1

Instead of checking every possible subarray, think about where the values 1 through k are positioned in the array.

Hint 2

If you know the leftmost and rightmost positions of values 1..k, what does it tell you about whether they form a contiguous block?

Hint 3

The minimum and maximum positions can be updated incrementally as k increases—you don't need to recompute them from scratch each time.