Back to problems

Check if each prefix forms 1..k permutation

Algorithm · Uber · Medium

Given an array arr of length n containing each integer from 1 to n exactly once, but in any order. For every prefix length k with $$1 \le k \le n$$, inspect the first k elements of arr. These k elements can be rearranged to match [1,2,...,k] exactly when they form the set {1,2,...,k}. Return an array res of length n, where res[k - 1] = 1 when the prefix of length k satisfies this condition, and 0 otherwise. The algorithm must run in $$O(n)$$ time and use only $$O(1)$$…

Checking your access…