You are given a 0-indexed integer array nums of length n.
The average difference of the index i is the absolute difference between the average of the first i + 1 elements of nums and the average of the last n - i - 1 elements. Both averages should be rounded down to the nearest integer.
Return the index with the minimum average difference. If there are multiple such indices, return the smallest one.
Note:
n elements is the sum of the n elements divided (integer division) by n.0 elements is considered to be 0.
Example 1:
Input: nums = [2,5,3,9,5,3] Output: 3 Explanation: - The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3. - The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2. - The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2. - The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0. - The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1. - The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4. The average difference of index 3 is the minimum average difference so return 3.
Example 2:
Input: nums = [0] Output: 0 Explanation: The only index is 0 so return 0. The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
Constraints:
1 <= nums.length <= 1050 <= nums[i] <= 105Problem Overview: You are given an integer array nums. For every index i, compute the average of the first i + 1 elements and the average of the remaining elements to the right. The goal is to return the index where the absolute difference between these two averages is minimized.
Approach 1: Brute Force Average Calculation (O(n2) time, O(1) space)
The straightforward approach evaluates every possible split point in the array. For each index i, iterate through the left part to compute its sum and average, then iterate through the right part to compute its sum and average. After computing both averages, take the absolute difference and track the smallest value seen so far. This works but repeatedly recomputes sums for overlapping subarrays, leading to quadratic time complexity. It is useful for understanding the definition of the problem but becomes too slow for large arrays.
Approach 2: Prefix Sum Technique (O(n) time, O(n) space)
The key observation is that averages depend on sums, and sums of prefixes can be reused. Build a prefix sum array where prefix[i] stores the sum of elements from index 0 to i. The total array sum is prefix[n-1]. For each index, compute the left average using prefix[i] / (i + 1) and the right average using (totalSum - prefix[i]) / (n - i - 1). If there are no elements on the right, the right average is defined as 0. Track the minimum absolute difference and its index as you iterate once through the array. This approach eliminates repeated summations and is the standard optimization when working with cumulative ranges using prefix sum. The iteration itself is linear, making the total time complexity O(n).
Approach 3: In-place Running Sums (O(n) time, O(1) space)
You can remove the extra prefix array by maintaining running sums during traversal. First compute the total sum of the array. Then iterate from left to right while keeping a leftSum. At index i, add nums[i] to leftSum, compute the right sum as totalSum - leftSum, and derive both averages using integer division. The left count is i + 1 and the right count is n - i - 1. Update the minimum difference and index during the scan. This approach keeps the same linear time as the prefix method but reduces memory usage to constant space. It’s a common optimization when solving problems on arrays where only cumulative totals are required.
Recommended for interviews: Interviewers expect the linear-time solution using prefix sums or running sums. Starting with the brute force explanation shows you understand the definition of the averages, but optimizing it with cumulative sums demonstrates algorithmic thinking and familiarity with the prefix sum pattern. The in-place running sum variant is usually considered the cleanest implementation because it achieves O(n) time with O(1) extra space.
The idea is to use prefix sums to efficiently calculate the sum of elements up to any index and from any index to the end.
First, compute the prefix sum of the array. Use this prefix sum to calculate the sum of the first i+1 elements and the sum of the last n-i-1 elements. The prefixed sums allow these sums to be computed in constant time. For the entire array, calculate the absolute difference in averages at each index, keep track of the minimum difference, and return its index.
This solution uses two loops: one for calculating the total sum of the array and the other for calculating the minimum average difference by using prefix sums. The prefix sums allow for a constant time calculation of the first i+1 elements and the last n-i-1 elements at each step.
Time Complexity: O(n), where n is the length of the array, since we iterate through the array twice.
Space Complexity: O(1), no extra space is used apart from variables to store sums.
This approach involves calculating total sum ahead of time and using a running sum in-place as we iterate through the array, allowing avoidance of additional space for prefix sums.
By subtracting the running sum from the total sum, we derive the right sum efficiently. Use these sums to calculate averages and determine minimal average difference efficiently.
In this approach, instead of explicitly creating a prefix sum array, we modify the running total inline during the loop, making it both time- and space-efficient.
Time Complexity: O(n)
Space Complexity: O(1)
We directly traverse the array nums. For each index i, we maintain the sum of the first i+1 elements pre and the sum of the last n-i-1 elements suf. We calculate the absolute difference of the average of the first i+1 elements and the average of the last n-i-1 elements, denoted as t. If t is less than the current minimum value mi, we update the answer ans=i and the minimum value mi=t.
After the traversal, we return the answer.
The time complexity is O(n), where n is the length of the array nums. The space complexity is O(1).
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Prefix Sum Technique | Time Complexity: O(n), where n is the length of the array, since we iterate through the array twice. |
| In-place Running Sums | Time Complexity: O(n) |
| Traverse | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Average Calculation | O(n^2) | O(1) | Useful for understanding the definition of prefix/suffix averages or validating small inputs |
| Prefix Sum Technique | O(n) | O(n) | When prefix arrays are acceptable and you want clear separation of cumulative sums |
| In-place Running Sums | O(n) | O(1) | Best for interviews and memory-constrained environments |
Minimum Average Difference-(Amazon, Paytm) : Explanation ➕ Live Coding • codestorywithMIK • 5,791 views views
Watch 9 more video solutions →Practice Minimum Average Difference with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor