Split Array With Same Average - Problem

You are given an integer array nums. You should move each element of nums into one of the two arrays A and B such that A and B are non-empty, and average(A) == average(B).

Return true if it is possible to achieve that and false otherwise.

Note: For an array arr, average(arr) is the sum of all the elements of arr over the length of arr.

Input & Output

Example 1 — Possible Split
$ Input: nums = [1,2,3,4,5,6,7,8]
Output: true
💡 Note: We can split into {1,4,5,8} with average 4.5 and {2,3,6,7} with average 4.5
Example 2 — Impossible Split
$ Input: nums = [3,1]
Output: false
💡 Note: Cannot split 2 elements into two non-empty groups with same average
Example 3 — Equal Elements
$ Input: nums = [1,1,1,1]
Output: true
💡 Note: Any split like {1,1} and {1,1} will have the same average of 1.0

Constraints

  • 1 ≤ nums.length ≤ 30
  • 0 ≤ nums[i] ≤ 104

Visualization

Tap to expand
Split Array With Same Average: [1,2,3,4,5,6]123456Total sum = 21, Total average = 3.5Group A: {1, 4, 5}Sum = 10, Count = 3Average = 3.33Group B: {2, 3, 6}Sum = 11, Count = 3Average = 3.67✗ Averages don't match (3.33 ≠ 3.67)Need to try different partitions...Solution: Try {1,5,6} avg=4 and {2,3,4} avg=3 - still no match
Understanding the Visualization
1
Input Array
Array [1,2,3,4,5,6] with total sum 21, avg 3.5
2
Find Partition
Look for two non-empty subsets with equal averages
3
Verify Solution
Check if {1,4,5} avg=3.33 and {2,3,6} avg=3.67
Key Takeaway
🎯 Key Insight: If avg(A) = avg(B), then both equal the total average sum/n
Asked in
Google 8 Facebook 5 Microsoft 3
22.3K Views
Medium Frequency
~45 min Avg. Time
890 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