Back to problems Sign in to unlock
Leetcode
Easy
Arrays
Greedy
Simulation
Distribute Elements Into Two Arrays I
Split the input into two arrays by repeatedly appending each next element to the array whose last element is larger.
Acceptance 0%
Problem Statement
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]:
- If one of the arrays is empty, append the number to that array.
- Otherwise, compare the last elements of
arr1andarr2.- Append
nums[i]to the array whose last element is greater. - If the last elements are equal, append the number to
arr1.
- Append
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.
Input Format
- An integer array
nums.
The problem is processed in order from nums[0] to nums[n-1].
Output Format
- Return the concatenation of
arr1andarr2after all elements have been assigned.
Constraints
1 <= nums.lengthnums[i]are integers- The exact official constraints are not provided here; the task is small and intended for direct simulation.
Examples
Sample cases returned by the problem API.
Example 1
Input
nums = [2,1,3,3]
Output
[2,3,1,3]
Explanation
- Start with
arr1 = [],arr2 = []. 2goes toarr1because it is empty.1goes toarr2becausearr1ends with2andarr2is empty.3goes toarr1because2 > 1.3goes toarr2because3 == 1is false and3 > 1, so it is appended to the array with the larger last element after the previous step, givingarr2 = [1,3].- Concatenate
arr1 + arr2 = [2,3,1,3].
Premium problem context
Unlock deeper context for this problem
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.
Guided hints
Editorial and discussion links
Concept map and variants
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.