Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 summary: 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: The absolute difference of two numbers is the absolute value of their difference. The average of n elements is the sum of the n elements divided (integer division) by n. The average of 0 elements is considered to be 0.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[2,5,3,9,5,3]
[0]
split-array-with-same-average)number-of-ways-to-split-array)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2256: Minimum Average Difference
class Solution {
public int minimumAverageDifference(int[] nums) {
int n = nums.length;
long pre = 0, suf = 0;
for (int x : nums) {
suf += x;
}
int ans = 0;
long mi = Long.MAX_VALUE;
for (int i = 0; i < n; ++i) {
pre += nums[i];
suf -= nums[i];
long a = pre / (i + 1);
long b = n - i - 1 == 0 ? 0 : suf / (n - i - 1);
long t = Math.abs(a - b);
if (t < mi) {
ans = i;
mi = t;
}
}
return ans;
}
}
// Accepted solution for LeetCode #2256: Minimum Average Difference
func minimumAverageDifference(nums []int) (ans int) {
n := len(nums)
pre, suf := 0, 0
for _, x := range nums {
suf += x
}
mi := suf
for i, x := range nums {
pre += x
suf -= x
a := pre / (i + 1)
b := 0
if n-i-1 != 0 {
b = suf / (n - i - 1)
}
if t := abs(a - b); t < mi {
ans = i
mi = t
}
}
return
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
# Accepted solution for LeetCode #2256: Minimum Average Difference
class Solution:
def minimumAverageDifference(self, nums: List[int]) -> int:
pre, suf = 0, sum(nums)
n = len(nums)
ans, mi = 0, inf
for i, x in enumerate(nums):
pre += x
suf -= x
a = pre // (i + 1)
b = 0 if n - i - 1 == 0 else suf // (n - i - 1)
if (t := abs(a - b)) < mi:
ans = i
mi = t
return ans
// Accepted solution for LeetCode #2256: Minimum Average Difference
/**
* [2256] Minimum Average Difference
*
* 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:
*
* The absolute difference of two numbers is the absolute value of their difference.
* The average of n elements is the sum of the n elements divided (integer division) by n.
* The average of 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 <= 10^5
* 0 <= nums[i] <= 10^5
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/minimum-average-difference/
// discuss: https://leetcode.com/problems/minimum-average-difference/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn minimum_average_difference(nums: Vec<i32>) -> i32 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2256_example_1() {
let nums = vec![2, 5, 3, 9, 5, 3];
let result = 3;
assert_eq!(Solution::minimum_average_difference(nums), result);
}
#[test]
#[ignore]
fn test_2256_example_2() {
let nums = vec![2, 5, 3, 9, 5, 3];
let result = 0;
assert_eq!(Solution::minimum_average_difference(nums), result);
}
}
// Accepted solution for LeetCode #2256: Minimum Average Difference
function minimumAverageDifference(nums: number[]): number {
const n = nums.length;
let pre = 0;
let suf = nums.reduce((a, b) => a + b);
let ans = 0;
let mi = suf;
for (let i = 0; i < n; ++i) {
pre += nums[i];
suf -= nums[i];
const a = Math.floor(pre / (i + 1));
const b = n - i - 1 === 0 ? 0 : Math.floor(suf / (n - i - 1));
const t = Math.abs(a - b);
if (t < mi) {
ans = i;
mi = t;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.