Distribute Elements Into Two Arrays II - Problem

You're tasked with distributing elements from an array into two separate arrays using a greedy algorithm based on counting strategy!

Given a 1-indexed array nums, you need to distribute all elements between two arrays arr1 and arr2 following these rules:

  • Operation 1: Add nums[1] to arr1
  • Operation 2: Add nums[2] to arr2
  • Operation i (i ≥ 3): Count elements strictly greater than nums[i] in both arrays using function greaterCount(arr, val)

For each subsequent element, choose the array with:

  1. Higher count of elements greater than current element
  2. If counts are equal, choose array with fewer elements
  3. If still tied, choose arr1

Example: If nums = [2,1,3,3]

  • Step 1: arr1 = [2], arr2 = []
  • Step 2: arr1 = [2], arr2 = [1]
  • Step 3: For nums[3] = 3: greaterCount(arr1, 3) = 0, greaterCount(arr2, 3) = 0 → tie, but arr1 and arr2 same size → choose arr1
  • Final: concatenate arrays to get result

Input & Output

example_1.py — Basic Distribution
$ Input: [2,1,3,3]
Output: [2,3,1,3]
💡 Note: Step 1: arr1=[2], arr2=[]. Step 2: arr1=[2], arr2=[1]. Step 3: For 3, count1=0, count2=0, sizes equal → choose arr1. Step 4: For 3, count1=0, count2=0, arr1 has more elements → choose arr2.
example_2.py — Count-based Decision
$ Input: [5,4,3,8]
Output: [5,3,4,8]
💡 Note: Step 1: arr1=[5], arr2=[]. Step 2: arr1=[5], arr2=[4]. Step 3: For 3, count1=1 (5>3), count2=1 (4>3), sizes equal → choose arr1. Step 4: For 8, count1=0, count2=0, arr2 smaller → choose arr2.
example_3.py — Single Element
$ Input: [1]
Output: [1]
💡 Note: Only one element, goes directly to arr1. Final result is just [1].

Constraints

  • 1 ≤ nums.length ≤ 105
  • 1 ≤ nums[i] ≤ 109
  • nums is 1-indexed in the problem statement

Visualization

Tap to expand
Strategic Player Distribution TournamentTeam 1 (arr1)23Skill: 2Skill: 3Team 2 (arr2)1Skill: 13New Player (Skill: 3)Count stronger: 0(no player > 3)Count stronger: 0(no player > 3)Decision Logiccount1 == count2 & size1 == size2→ Choose Team 1 (default tie-breaker)Join Team 1!🚀 Optimization: Binary Indexed TreeCount stronger players in O(log n) instead of O(n)💡 Result: O(n²) → O(n log n) time complexity improvement!
Understanding the Visualization
1
Initial Setup
Place first player in Team 1, second player in Team 2
2
Count Stronger Players
For each new player, count existing stronger players in each team
3
Make Decision
Join team with more stronger players, break ties by smaller team size
4
Optimize Counting
Use Binary Indexed Tree instead of linear counting for efficiency
Key Takeaway
🎯 Key Insight: Transform the O(n) counting problem into O(log n) range queries using Binary Indexed Trees, dramatically improving efficiency from O(n²) to O(n log n).
Asked in
Meta 45 Google 38 Amazon 32 Microsoft 28
37.4K Views
Medium-High Frequency
~25 min Avg. Time
1.6K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen