Skip to main content
Back to problems
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 arr1 and arr2.
    • Append nums[i] to the array whose last element is greater.
    • If the last elements are equal, append the number to 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.

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 arr1 and arr2 after all elements have been assigned.

Constraints

  • 1 <= nums.length
  • nums[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 = [].
  • 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].
  • 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
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.