-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path4 DEC 2256. Minimum Average Difference
43 lines (36 loc) · 1.52 KB
/
4 DEC 2256. Minimum Average Difference
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Solution {
public:
int minimumAverageDifference(vector<int>& nums) {
int n = int(nums.size());
int ans = -1;
int minAvgDiff = numeric_limits<int>::max();
// Generate prefix and suffix sum arrays.
vector<long long> prefixSum(n + 1);
vector<long long> suffixSum(n + 1);
for (int index = 0; index < n; ++index) {
prefixSum[index + 1] = prefixSum[index] + nums[index];
}
for (int index = n - 1; index >= 0; --index) {
suffixSum[index] = suffixSum[index + 1] + nums[index];
}
for (int index = 0; index < n; ++index) {
// Calculate average of left part of array, index 0 to i.
long long leftPartAverage = prefixSum[index + 1];
leftPartAverage /= (index + 1);
// Calculate average of right part of array, index i+1 to n-1.
long long rightPartAverage = suffixSum[index + 1];
// If right part have 0 elements, then we don't divide by 0.
if (index != n - 1) {
rightPartAverage /= (n - index - 1);
}
int currDifference = int(abs(leftPartAverage - rightPartAverage));
// If current difference of averages is smaller,
// then current index can be our answer.
if (currDifference < minAvgDiff) {
minAvgDiff = currDifference;
ans = index;
}
}
return ans;
}
};