Skip to content

Practice Distribute Elements Into Two Arrays II

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#3072
LeetCode Problem

Distribute Elements Into Two Arrays II

Problem Statement

Treat nums as 1-indexed. Start arr1 with nums[1] and arr2 with nums[2], then process each remaining value in order. Define greaterCount(arr, value) as the number of entries in arr strictly greater than value. Append the new value to the array with the larger greaterCount; if tied, choose the shorter array; if still tied, choose arr1. Return arr1 followed by arr2, preserving order within each.

Source: Distribute Elements Into Two Arrays II on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [2,1,3,3]
Output
[2,3,1,3]

# Example 2

Input
nums = [5,14,3,1,2]
Output
[5,3,1,2,14]

# Example 3

Input
nums = [3,3,3,3]
Output
[3,3,3,3]

Constraints

3 <= n <= 10^5
1 <= nums[i] <= 10^9

Related LeetCode Problems

Practice more problems similar to Distribute Elements Into Two Arrays II.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.