Split the input into two arrays by repeatedly appending each next element to the array whose last element is larger.
You are given an integer array nums. Build two arrays, arr1 and arr2, by processing nums from left to right.
For each number nums[i]:
arr1 and arr2.
nums[i] to the array whose last element is greater.arr1.After all numbers are processed, return the final array formed by concatenating arr1 followed by arr2.
This is a straightforward greedy simulation problem: at each step, make the locally prescribed choice and continue.
nums.The problem is processed in order from nums[0] to nums[n-1].
arr1 and arr2 after all elements have been assigned.1 <= nums.lengthnums[i] are integersExample 1
Input
nums = [2,1,3,3]
Output
[2,3,1,3]
Explanation
arr1 = [], arr2 = [].2 goes to arr1 because it is empty.1 goes to arr2 because arr1 ends with 2 and arr2 is empty.3 goes to arr1 because 2 > 1.3 goes to arr2 because 3 == 1 is false and 3 > 1, so it is appended to the array with the larger last element after the previous step, giving arr2 = [1,3].arr1 + arr2 = [2,3,1,3].Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.