Determine which values k from 1 to n are balanced in a permutation.
Unlock detailed company stats for this questionUpgrade
Given a permutation p of length n, a number k is balanced if there exist indices l <= r such that p[l..r] form a permutation of 1..k. For each k (1 <= k <= n), determine if it is balanced and return a binary string of length n where the i-th character is '1' if i is balanced. Constraints: 1 <= n <= 2*10^5, all p[i] distinct. Examples: n=5, p=[5,3,1,2,4] -> '11111'; n=4, p=[1,4,2,3] -> '1001'; n=4, p=[4,1,3,2] -> '1011'. Approach: track min/max positions of values 1..k; k is balanced iff maxPos - minPos + 1 == k. O(n).
Format · Microsoft SDE-2 HackerRank OA, 90 minutes, 2 questions.
Community answers
No answers contributed by the community yet.
Related questions
Return a binary string marking which values k are balanced in an array.Balanced prefixes of a permutation.Related courses

Course

Course
Course
