Split Array With Same Average - Problem

Given an integer array nums, you need to determine if it's possible to split the array into two non-empty groups such that both groups have the same average.

Think of this as dividing a class of students with different test scores into two groups where both groups have identical average scores. You must move each student to exactly one of the two groups, and both groups must have at least one student.

Goal: Return true if such a split is possible, false otherwise.

Note: The average of an array is the sum of all elements divided by the length of the array.

Example: For nums = [1,2,3,4,5,6,7,8], we can split into [1,4,5,8] and [2,3,6,7]. Both groups have average 4.5!

Input & Output

example_1.py — Basic Split
$ Input: [1,2,3,4,5,6,7,8]
Output: true
💡 Note: We can split into [1,4,5,8] (sum=18, avg=4.5) and [2,3,6,7] (sum=18, avg=4.5). Both groups have the same average of 4.5.
example_2.py — Impossible Split
$ Input: [3,1]
Output: false
💡 Note: We can only split into [3] (avg=3) and [1] (avg=1). Since 3 ≠ 1, it's impossible to achieve equal averages.
example_3.py — Multiple Valid Splits
$ Input: [1,2,3,4]
Output: false
💡 Note: Total sum is 10. For equal averages, we need sum₁/size₁ = sum₂/size₂ = 2.5. But with integers, we can't achieve fractional sums, so it's impossible.

Visualization

Tap to expand
Split Array with Same AverageGroup A{1, 4, 5, 8}Sum: 18, Size: 4, Avg: 4.5Group B{2, 3, 6, 7}Sum: 18, Size: 4, Avg: 4.5Mathematical Constraintsum₁/size₁ = sum₂/size₂18/4 = 18/4 = 4.5 ✓Required: sum₁ = (36 × 4)/8 = 18Algorithm Steps1. Calculate total sum = 36, n = 82. For k=4: required sum = (36 × 4) ÷ 8 = 18 ✓3. Use DP to check: Can we pick 4 numbers summing to 18?4. DP confirms: {1,4,5,8} works! Return true.
Understanding the Visualization
1
Understand the constraint
For equal averages: sum₁/size₁ = sum₂/size₂ = total_sum/n
2
Derive the formula
This means sum₁ = (total_sum × size₁) / n must be an integer
3
Check feasibility
For each possible group size k, check if (total_sum × k) % n == 0
4
Use dynamic programming
If feasible, use DP to check if we can actually form such a subset
Key Takeaway
🎯 Key Insight: Equal averages occur when subset sum equals (total_sum × subset_size) ÷ array_length. Use math to find targets, then DP to verify feasibility!

Time & Space Complexity

Time Complexity
⏱️
O(2^n)

We generate all 2^n possible subsets of the array

n
2n
Quadratic Growth
Space Complexity
O(n)

Recursion stack depth and space for storing current subset

n
2n
Linearithmic Space

Constraints

  • 1 ≤ nums.length ≤ 30
  • 0 ≤ nums[i] ≤ 104
  • Both groups must be non-empty
  • The average is calculated as sum of elements divided by count
Asked in
Google 15 Meta 12 Amazon 8 Microsoft 6
33.3K Views
Medium Frequency
~35 min Avg. Time
892 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