Given a permutation p containing the integers from 1 through n, examine every k in the range 1 to n. A value of k is balanced when some contiguous subarray of p has length exactly k and contains the values 1, 2, ..., k, in any ordering.
Produce an integer array result with n entries. At position k - 1, place 1 when k is balanced; otherwise, place 0.
Constraints:
p.length ≤ $$10^5$1 ≤ p[i] ≤ np is distinctExample 1:
p = [2, 4, 1, 3]
[1, 0, 0, 1]
For k=1: value 1 is at index 2, so it forms a length-1 subarray by itself → 1 For k=2: values {1, 2} occur at indices 2 and 0, whose span is 3 rather than 2 → 0 For k=3: values {1, 2, 3} occur at indices 0, 2, and 3, whose span is 4 rather than 3 → 0 For k=4: the complete array covers indices 0..3, giving a span of 4 → 1
Example 2:
p = [5, 3, 1, 2, 4]
[1, 1, 1, 1, 1]
Example 3:
p = [1, 3, 2, 4]
[1, 0, 1, 1]
Example 1
Input:
4
2 4 1 3
Output: 1 0 0 1
Example 2
Input:
5
5 3 1 2 4
Output: 1 1 1 1 1