Minimum Average Difference - Problem
Find the Optimal Split Point!
You're given an array of integers, and your task is to find the best place to "split" it. For any index
š¹ Left part: First
š¹ Right part: Remaining
Calculate the average difference at index
⢠Average of left part (rounded down)
⢠Average of right part (rounded down)
Goal: Return the index with the minimum average difference. If there are ties, return the smallest index.
Special cases:
⢠Use integer division (round down) for averages
⢠If right part is empty, its average is considered 0
Example: For array
⢠Left: [2,5,3] ā average = 10/3 = 3
⢠Right: [9,5,3] ā average = 17/3 = 5
⢠Difference = |3 - 5| = 2
You're given an array of integers, and your task is to find the best place to "split" it. For any index
i, you can divide the array into two parts:š¹ Left part: First
i + 1 elementsš¹ Right part: Remaining
n - i - 1 elementsCalculate the average difference at index
i as the absolute difference between:⢠Average of left part (rounded down)
⢠Average of right part (rounded down)
Goal: Return the index with the minimum average difference. If there are ties, return the smallest index.
Special cases:
⢠Use integer division (round down) for averages
⢠If right part is empty, its average is considered 0
Example: For array
[2,5,3,9,5,3], at index 2:⢠Left: [2,5,3] ā average = 10/3 = 3
⢠Right: [9,5,3] ā average = 17/3 = 5
⢠Difference = |3 - 5| = 2
Input & Output
example_1.py ā Basic Case
$
Input:
nums = [2,5,3,9,5,3]
āŗ
Output:
3
š” Note:
At index 3: Left part [2,5,3,9] has average 19/4=4, Right part [5,3] has average 8/2=4. Difference = |4-4| = 0, but we need to check all positions. At index 3, we get the minimum difference.
example_2.py ā Single Element
$
Input:
nums = [0]
āŗ
Output:
0
š” Note:
Only one element, so we can only split at index 0. Left part [0] has average 0, right part is empty (average 0). Difference = 0.
example_3.py ā Two Elements
$
Input:
nums = [1,2]
āŗ
Output:
1
š” Note:
Index 0: Left=[1] avg=1, Right=[2] avg=2, diff=1. Index 1: Left=[1,2] avg=1, Right=[] avg=0, diff=1. Both have same difference, return smaller index 0... wait, let me recalculate. Actually index 1 gives diff=1, index 0 gives diff=1, so return 0.
Visualization
Tap to expand
Understanding the Visualization
1
Calculate Total
First, calculate the total sum of all elements: 2+5+3+9+5+3 = 27
2
Try Each Split
For each position, calculate left and right averages using prefix sums
3
Track Minimum
Keep track of the position with minimum absolute difference
4
Return Result
Return the index with minimum difference (smallest index if tied)
Key Takeaway
šÆ Key Insight: Using prefix sums eliminates redundant calculations, reducing time complexity from O(n²) to O(n) while maintaining O(1) space complexity.
Time & Space Complexity
Time Complexity
O(n²)
For each of n positions, we sum up to n elements, giving us n Ć n operations
ā Quadratic Growth
Space Complexity
O(1)
Only using a few variables to track sums and minimum difference
ā Linear Space
Constraints
- 1 ⤠nums.length ⤠105
- 0 ⤠nums[i] ⤠105
- All averages are calculated using integer division (rounded down)
- If there are multiple indices with the same minimum difference, return the smallest one
š”
Explanation
AI Ready
š” Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code