Balanced Prefix Sets in a Permutation
You are given a permutation p of the integers 1 through n.
For each k from 1 to n, determine whether there exists a contiguous subarray whose set of values is exactly {1, 2, ..., k}.
Build a binary string of length n. The k-th character should be '1' if such a subarray exists for that k, otherwise it should be '0'.
Complete the function balancedPrefixSetsMask in the editor below.
balancedPrefixSetsMask has the following parameter:
int[] p: a permutation of1..n
Returns
String: a binary string whose k-th character indicates whether {1, 2, ..., k} appears as the value set of some contiguous subarray.
1Example 1
For k = 1, the subarray [1] works. For k = 2, the subarray [2, 1] works. For k = 3, the values {1, 2, 3} do not occupy a contiguous span. For k = 4, the whole array works.
2Example 2
The sets {1}, {1, 2}, and {1, 2, 3} all appear within contiguous spans. The set {1, 2, 3, 4} does not. The whole array gives {1, 2, 3, 4, 5}.
Constraints
Limits and guarantees your solution can rely on.
The source thread did not provide explicit numeric bounds.
pis a permutation of the integers1..n.- The output string has length
n. - Each character is either
'1'or'0'.