[Hacker Earth] Monk and OtakulandRead more
[Hacker Earth] Candy Distribution 3Read more
[Hacker Earth] XOR in SequenceRead more
[Hacker Earth] Count NumbersRead more
[Codeforces] Educational Round 44 (Rated for Div. 2) E. Pencils and BoxesRead more
[Codeforces] Round 486 (Div. 3) E. Divisibility by 25Read more
[Codeforces] Round 486 (Div. 3) F. Rain and UmbrellasRead more
[Codeforces] Educational Round 45 (Rated for Div. 2) E. Post LampsRead more
[Codeforces] Round 489 (Div. 2) D. Nastya and a GameRead more
[LeetCode] Distribute Elements Into Two Arrays II

3072. Distribute Elements Into Two Arrays II

You are given a 1-indexed array of integers nums of length n.

We define a function greaterCount such that greaterCount(arr, val) returns the number of elements in arr that are strictly greater than val.

You need to distribute all the elements of nums between two arrays arr1 and arr2 using n operations. In the first operation, append nums[1] to arr1. In the second operation, append nums[2] to arr2. Afterwards, in the ith operation:

  • 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 a lesser number of elements.
  • If there is still a tie, append nums[i] to arr1.

The array result is formed by concatenating the arrays arr1 and arr2. For example, if arr1 == [1,2,3] and arr2 == [4,5,6], then result = [1,2,3,4,5,6].

Return the integer array result.

Read more