Welcome to Subscribe On Youtube
Formatted question description: https://leetcode.ca/all/2256.html
2256. Minimum Average Difference
- Difficulty: Medium.
- Related Topics: Array, Prefix Sum.
- Similar Questions: Split Array With Same Average, Number of Ways to Split Array.
Problem
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 then
elements divided (integer division) byn
. -
The average of
0
elements is considered to be0
.
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 <= 105
-
0 <= nums[i] <= 105
Solution (Java, C++, Python)
-
class Solution { public int minimumAverageDifference(int[] nums) { long numsSum = 0; for (int num : nums) { numsSum += num; } long minAverageDiff = Long.MAX_VALUE; long sumFromFront = 0; int index = 0; for (int i = 0; i < nums.length; i++) { sumFromFront += nums[i]; int numbersRight = i == nums.length - 1 ? 1 : nums.length - i - 1; long averageDiff = Math.abs(sumFromFront / (i + 1) - (numsSum - sumFromFront) / numbersRight); if (minAverageDiff > averageDiff) { minAverageDiff = averageDiff; index = i; } if (averageDiff == 0) { break; } } return index; } } ############ class Solution { public int minimumAverageDifference(int[] nums) { int n = nums.length; long[] s = new long[n]; s[0] = nums[0]; for (int i = 1; i < n; ++i) { s[i] = s[i - 1] + nums[i]; } int ans = 0; long mi = Long.MAX_VALUE; for (int i = 0; i < n; ++i) { long a = s[i] / (i + 1); long b = i == n - 1 ? 0 : (s[n - 1] - s[i]) / (n - i - 1); long t = Math.abs(a - b); if (mi > t) { ans = i; mi = t; } } return ans; } }
-
class Solution: def minimumAverageDifference(self, nums: List[int]) -> int: s = list(accumulate(nums)) ans, n = 0, len(nums) mi = inf for i in range(n): a = s[i] // (i + 1) b = 0 if i == n - 1 else (s[-1] - s[i]) // (n - i - 1) t = abs(a - b) if mi > t: ans = i mi = t return ans ############ # 2256. Minimum Average Difference # https://leetcode.com/problems/minimum-average-difference/ class Solution: def minimumAverageDifference(self, nums: List[int]) -> int: mmin = float('inf') res = -1 n = len(nums) curr = 0 s = sum(nums) for i, x in enumerate(nums): left = i + 1 right = n - i - 1 curr += x s -= x if i == n - 1: ans = abs(floor(curr / left)) else: ans = abs(floor(curr / left) - floor(s / right)) # print(i, ans) if ans < mmin: mmin = ans res = i return res
-
typedef long long ll; class Solution { public: int minimumAverageDifference(vector<int>& nums) { int n = nums.size(); vector<ll> s(n); s[0] = nums[0]; for (int i = 1; i < n; ++i) s[i] = s[i - 1] + nums[i]; int ans = 0; ll mi = LONG_MAX; for (int i = 0; i < n; ++i) { ll a = s[i] / (i + 1); ll b = i == n - 1 ? 0 : (s[n - 1] - s[i]) / (n - i - 1); ll t = abs(a - b); if (mi > t) { ans = i; mi = t; } } return ans; } };
-
func minimumAverageDifference(nums []int) int { n := len(nums) s := make([]int, n) s[0] = nums[0] for i := 1; i < n; i++ { s[i] = s[i-1] + nums[i] } ans := 0 mi := math.MaxInt32 for i := 0; i < n; i++ { a := s[i] / (i + 1) b := 0 if i != n-1 { b = (s[n-1] - s[i]) / (n - i - 1) } t := abs(a - b) if mi > t { ans = i mi = t } } return ans } func abs(x int) int { if x < 0 { return -x } return x }
-
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; }
Explain:
nope.
Complexity:
- Time complexity : O(n).
- Space complexity : O(n).