Tap here to switch tabs
Problem
Submissions

Problem: Distribute Elements Into Two Arrays II

Try to solve the Distribute Elements Into Two Arrays II problem.

Statement

You are given an integer array nums of length n.

You need to distribute all elements of nums into two arrays, arr1 and arr2, using the following process:

  1. Append nums[0] to arr1.

  2. Append nums[1] to arr2.

  3. For each subsequent element nums[i] from index i=2i = 2 to n−1n - 1:

    • Let greaterCount(arr, val) be the number of elements in arr that are strictly greater than val.

    • If greaterCount(arr1, nums[i]) > greaterCount(arr2, nums[i]), append nums[i] to arr1.

    • If greaterCount(arr1, nums[i]) < greaterCount(arr2, nums[i]), append nums[i] to arr2.

    • If greaterCount(arr1, nums[i]) == greaterCount(arr2, nums[i]), append nums[i] to the array with the smaller length. If both arrays have the same length, append nums[i] to arr1.

Return the array formed by concatenating arr1 and arr2.

Constraints:

  • 3≤n≤1053 \leq n \leq 10^5

  • 1≤1 \leq nums[i] ≤109\leq 10^9

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Distribute Elements Into Two Arrays II

Try to solve the Distribute Elements Into Two Arrays II problem.

Statement

You are given an integer array nums of length n.

You need to distribute all elements of nums into two arrays, arr1 and arr2, using the following process:

  1. Append nums[0] to arr1.

  2. Append nums[1] to arr2.

  3. For each subsequent element nums[i] from index i=2i = 2 to n−1n - 1:

    • Let greaterCount(arr, val) be the number of elements in arr that are strictly greater than val.

    • If greaterCount(arr1, nums[i]) > greaterCount(arr2, nums[i]), append nums[i] to arr1.

    • If greaterCount(arr1, nums[i]) < greaterCount(arr2, nums[i]), append nums[i] to arr2.

    • If greaterCount(arr1, nums[i]) == greaterCount(arr2, nums[i]), append nums[i] to the array with the smaller length. If both arrays have the same length, append nums[i] to arr1.

Return the array formed by concatenating arr1 and arr2.

Constraints:

  • 3≤n≤1053 \leq n \leq 10^5

  • 1≤1 \leq nums[i] ≤109\leq 10^9