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.
p = [2, 1, 4, 3] return = "1101"
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.
p = [3, 1, 2, 5, 4] return = "11101"
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}.
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'.
- Jump Game with Prime-3 StepsOA · Seen Jun 2026
- Total Palindrome Substring CostOA · Seen Jun 2026
- Earliest Time All Users Are ConnectedPHONE SCREEN · Seen May 2026
- Tournament Rounds by RankPHONE SCREEN · Seen May 2026
- Farthest Seat AssignmentONSITE INTERVIEW · Seen May 2026
- Convex Function MinimizationPHONE SCREEN · Seen May 2026
- Maximal Square AreaONSITE INTERVIEW · Seen May 2026
- First Unique IP Hitting the ServerPHONE SCREEN · Seen May 2026
public String balancedPrefixSetsMask(int[] p) {
// write your code here
}