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 i, you can divide the array into two parts:

šŸ”¹ Left part: First i + 1 elements
šŸ”¹ Right part: Remaining n - i - 1 elements

Calculate 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
Minimum Average Difference Visualization253953Split at index 3Left GroupSum: 2+5+3+9 = 19Average: 19Ć·4 = 4Right GroupSum: 5+3 = 8Average: 8Ć·2 = 4Difference = |4 - 4| = 0This is the minimum!Key Insight: Use prefix sums to avoid recalculatingTime: O(n), Space: O(1)
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

n
2n
⚠ Quadratic Growth
Space Complexity
O(1)

Only using a few variables to track sums and minimum difference

n
2n
āœ“ 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
Asked in
Google 35 Amazon 28 Meta 22 Microsoft 18
21.7K Views
Medium Frequency
~15 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