Find Contiguous Prefix Values in Permutation
Arrays & HashingAuto-graded · 21 test casesLast 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,000permis a valid permutation of1ton- Each value from
1tonappears exactly once inperm
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.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Rather than checking every possible subarray for each value of k, we reverse the question: given the values 1 through k, where do they actually sit in the permutation?
Build a lookup table that maps each value to its index in the array. For any k, compute the minimum and maximum indices where values 1, 2, ..., k appear. Call this range [min_pos, max_pos].
The key insight is that k is contiguous if and only if max_pos - min_pos + 1 == k. Why? Because we have exactly k distinct values that must occupy k distinct positions. If the span from the leftmost to the rightmost position is exactly k, then every position in that range must be filled by one of our k values, with no gaps and no extra elements.
As we increment k from 1 to n, we only need to incorporate one new position—the index of value k. The minimum can only decrease (or stay the same), and the maximum can only increase (or stay the same). This allows us to update both in constant time per iteration.
Why It Works
For any set of k distinct positions in an array of length n, the smallest interval containing all of them has length at least k. If that interval has length exactly k, then it contains no other positions—meaning the k values occupy consecutive indices.
Conversely, if values 1..k do occupy some contiguous block, that block must span from the minimum to the maximum position of those values, giving a span of exactly k.
The value k = 1 is always contiguous since the single element with value 1 trivially forms a valid one-element segment.
Complexity
- Time: O(n). We make one pass to build the index lookup and one pass to check each
k. - Space: O(n) for the index array and result string.
This approach handles the upper constraint of n = 200,000 efficiently.
Solution
from typing import List
def find_contiguous_prefix_values(perm: List[int]) -> str:
n = len(perm)
# Build index lookup: where does each value appear?
index_of = [0] * (n + 1)
for idx, val in enumerate(perm):
index_of[val] = idx
# Track the span of positions containing values 1..k
min_pos = max_pos = index_of[1]
answer = ['0'] * n
answer[0] = '1' # k=1 is always contiguous
for k in range(2, n + 1):
current_pos = index_of[k]
min_pos = min(min_pos, current_pos)
max_pos = max(max_pos, current_pos)
# If the span length equals k, then positions are consecutive
if max_pos - min_pos + 1 == k:
answer[k - 1] = '1'
return ''.join(answer)