2256. Minimum Average Difference Medium
1/**
2 * [2256] Minimum Average Difference
3 *
4 * You are given a 0-indexed integer array nums of length n.
5 * 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.
6 * Return the index with the minimum average difference. If there are multiple such indices, return the smallest one.
7 * Note:
8 *
9 * The absolute difference of two numbers is the absolute value of their difference.
10 * The average of n elements is the sum of the n elements divided (integer division) by n.
11 * The average of 0 elements is considered to be 0.
12 *
13 *
14 * Example 1:
15 *
16 * Input: nums = [2,5,3,9,5,3]
17 * Output: 3
18 * Explanation:
19 * - The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
20 * - The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
21 * - The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
22 * - The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
23 * - The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
24 * - The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
25 * The average difference of index 3 is the minimum average difference so return 3.
26 *
27 * Example 2:
28 *
29 * Input: nums = [0]
30 * Output: 0
31 * Explanation:
32 * The only index is 0 so return 0.
33 * The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
34 *
35 *
36 * Constraints:
37 *
38 * 1 <= nums.length <= 10^5
39 * 0 <= nums[i] <= 10^5
40 *
41 */
42pub struct Solution {}
43
44// problem: https://leetcode.com/problems/minimum-average-difference/
45// discuss: https://leetcode.com/problems/minimum-average-difference/discuss/?currentPage=1&orderBy=most_votes&query=
46
47// submission codes start here
48
49impl Solution {
50 pub fn minimum_average_difference(nums: Vec<i32>) -> i32 {
51 let mut sums = vec![];
52 let mut sum: i64 = 0;
53
54 for n in &nums {
55 sum += *n as i64;
56 sums.push(sum);
57 }
58
59 let mut min = i64::MAX;
60 let mut result = 0;
61
62 for i in 0..nums.len() {
63 let left = sums[i] / (i as i64 + 1);
64
65 let right = (sum - sums[i])
66 / if i < sums.len() - 1 {
67 sums.len() - i - 1
68 } else {
69 1
70 } as i64;
71
72 let current = (left - right).abs();
73
74 if current < min {
75 min = current;
76 result = i;
77 }
78 }
79
80 return result as i32;
81 }
82}
83
84// submission codes end
85
86#[cfg(test)]
87mod tests {
88 use super::*;
89
90 #[test]
91 fn test_2256_1() {
92 assert_eq!(
93 Solution::minimum_average_difference(vec!(2, 5, 3, 9, 5, 3)),
94 3
95 );
96 }
97}
98
Back
© 2025 bowen.ge All Rights Reserved.