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
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code