My first instinct was something like sorting or a set membership check per k, which would've been way too slow.
For each k, a contiguous subarray that is a permutation of 1..k must contain exactly the elements 1..k and have length k. Track the minimum and maximum positions of elements 1..k as you iterate k from 1 to n; if the difference between max and min positions equals k-1, then the elements occupy a contiguous block, so the answer is '1'. This yields an O(n) solution.
Pro tip: Mention that the condition is both necessary and sufficient: if the span of positions of 1..k is exactly k-1, the elements must be exactly 1..k (since they are distinct), so no additional checks are needed. This shows you understand the invariant and avoids overcomplicating the solution.
Clarify that a contiguous subarray that is a permutation of 1..k must contain each number from 1 to k exactly once, and thus its length is k. The subarray can be anywhere in the permutation.
For a given k, the elements 1..k occupy positions that must form a contiguous block of length k. This means the difference between the maximum and minimum positions of these elements must be exactly k-1.
Precompute the position of each value in the permutation. Iterate k from 1 to n, maintaining the minimum and maximum positions of elements 1..k. For each k, check if maxPos - minPos == k-1; if so, append '1', else '0'.
The algorithm runs in O(n) time and O(n) space. Handle edge cases like n=1 (always '1') and ensure the logic works when the subarray is at the beginning or end.
Walk through a small example (e.g., permutation [2,1,3]) to verify the output. For k=1, positions of 1 is 2, span 0 -> '1'; k=2, positions of 1 and 2 are 2 and 1, span 1 -> '1'; k=3, positions 2,1,3 span 2 -> '1'. Output '111'.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.