We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent ed8ca44 commit 9bc4661Copy full SHA for 9bc4661
Medium/Minimum Average Difference.java
@@ -0,0 +1,23 @@
1
+class Solution {
2
+ public int minimumAverageDifference(int[] nums) {
3
+ int n = nums.length;
4
+ long[] prefixSumArray = new long[n];
5
+ long currSum = 0;
6
+ for (int i = 0; i < n; i++) {
7
+ currSum += nums[i];
8
+ prefixSumArray[i] = currSum;
9
+ }
10
+ long minDifference = Integer.MAX_VALUE;
11
+ int minDifferenceIdx = -1;
12
13
+ long leftAverage = prefixSumArray[i] / (i + 1);
14
+ long rightAverage = (i == n - 1) ? 0 : (prefixSumArray[n - 1] - prefixSumArray[i]) / (n - 1 - i);
15
+ long absoluteDiff = Math.abs(leftAverage - rightAverage);
16
+ if (minDifference > absoluteDiff) {
17
+ minDifference = absoluteDiff;
18
+ minDifferenceIdx = i;
19
20
21
+ return minDifferenceIdx;
22
23
+}
0 commit comments