Description
Solutions
Array Reduction Algorithm
🤘 INTERN📚RELATED PROBLEMS
The developers of Hackerland are working on an array reduction algorithm that takes in an array of n
integers say arr
and does the following until the array arr
is empty:
result
as an empty arrayk
such that 1 ≤ k ≤ length of the array
k
elements of the array arr
to the array result
k
elements of the array arr
Given an array arr
, find the lexicographically maximum array result
that can be obtained using the above algorithm.
Note:
x
is lexicographically greater than an array y
if in the first position where x
and y
differ xi > yi
or if |x| > |y|
and y
is a prefix of x
(where |x|
denotes the size of the array x
).Example 1:
Input: arr = [0,1,1,0]
Output: [2,2]
Explanation:Givenn = 4
,arr = [0,1,1,0]
, one of the optimal ways to make arrayresult
lexicographically maximum is as follows -Take k = 2
, the MEX of the 1st and 2nd element ofarr
is 2. Soarr = [1,0]
andresult = [2]
.Take k = 2
, the MEX of the 1st and 2nd element ofarr
is 2. Soarr = []
andresult = [2,2]
.arr
is now empty and the answer is[2,2]
.
Constraints:
- 1 <= n <= 10^5
- 0 <= arr[i] <= n


Related Problems
Testcase
Result
Case 1
input:
output: