|
| 1 | +/* |
| 2 | +
|
| 3 | +
|
| 4 | +-* 2256. Minimum Average Difference *- |
| 5 | +
|
| 6 | +You are given a 0-indexed integer array nums of length n. |
| 7 | +
|
| 8 | +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. |
| 9 | +
|
| 10 | +Return the index with the minimum average difference. If there are multiple such indices, return the smallest one. |
| 11 | +
|
| 12 | +Note: |
| 13 | +
|
| 14 | +The absolute difference of two numbers is the absolute value of their difference. |
| 15 | +The average of n elements is the sum of the n elements divided (integer division) by n. |
| 16 | +The average of 0 elements is considered to be 0. |
| 17 | + |
| 18 | +
|
| 19 | +Example 1: |
| 20 | +
|
| 21 | +Input: nums = [2,5,3,9,5,3] |
| 22 | +Output: 3 |
| 23 | +Explanation: |
| 24 | +- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3. |
| 25 | +- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2. |
| 26 | +- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2. |
| 27 | +- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0. |
| 28 | +- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1. |
| 29 | +- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4. |
| 30 | +The average difference of index 3 is the minimum average difference so return 3. |
| 31 | +Example 2: |
| 32 | +
|
| 33 | +Input: nums = [0] |
| 34 | +Output: 0 |
| 35 | +Explanation: |
| 36 | +The only index is 0 so return 0. |
| 37 | +The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0. |
| 38 | + |
| 39 | +
|
| 40 | +Constraints: |
| 41 | +
|
| 42 | +1 <= nums.length <= 105 |
| 43 | +0 <= nums[i] <= 105 |
| 44 | +
|
| 45 | +
|
| 46 | +
|
| 47 | +
|
| 48 | +
|
| 49 | +*/ |
| 50 | + |
| 51 | +import 'dart:math'; |
| 52 | + |
| 53 | +class A { |
| 54 | + int minimumAverageDifference(List<int> nums) { |
| 55 | + int ans = 0, n = nums.length, mi = double.maxFinite.toInt(); |
| 56 | + // since we need the sum for first i + 1 and last n - i - 1 elements |
| 57 | + // we can pre-calculate it first |
| 58 | + // it is called prefix sum and suffix sum |
| 59 | + List<int> pref = List.filled(n, 0); |
| 60 | + // prev[0] is the first element |
| 61 | + pref[0] = nums[0]; |
| 62 | + // starting from i = 1, pref[i] is the sum + the current element |
| 63 | + for (int i = 1; i < n; i++) pref[i] = pref[i - 1] + nums[i]; |
| 64 | + // then we can iterate each number |
| 65 | + for (int i = 0; i < n; i++) { |
| 66 | + // now we know the prefix sum |
| 67 | + // the suffix sum is simply pref[n - 1] - pref[i] |
| 68 | + int k = ((pref[i] ~/ (i + 1)) - |
| 69 | + ((pref[n - 1] - pref[i]) ~/ max(n - i - 1, 1))) |
| 70 | + .abs(); |
| 71 | + // check the min and update ans |
| 72 | + if (k < mi) { |
| 73 | + mi = k; |
| 74 | + ans = i; |
| 75 | + } |
| 76 | + } |
| 77 | + return ans; |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +class B { |
| 82 | + int minimumAverageDifference(List<int> nums) { |
| 83 | + int n = nums.length; |
| 84 | + |
| 85 | + int sum = 0; |
| 86 | + for (int num in nums) sum += num; |
| 87 | + |
| 88 | + int minDiff = double.maxFinite.toInt(); |
| 89 | + int leftSum = 0; |
| 90 | + int rightSum = sum; |
| 91 | + int ans = 0; |
| 92 | + |
| 93 | + for (int i = 0; i < n; i++) { |
| 94 | + leftSum += nums[i]; |
| 95 | + rightSum -= nums[i]; |
| 96 | + int lAvg = leftSum ~/ (i + 1); |
| 97 | + int rAvg = (n - 1 - i) != 0 ? rightSum ~/ (n - 1 - i) : rightSum; |
| 98 | + int diff = (lAvg - rAvg).abs(); |
| 99 | + if (diff == 0) return i; |
| 100 | + if (diff < minDiff) { |
| 101 | + minDiff = diff; |
| 102 | + ans = i; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return ans; |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +class C { |
| 111 | + int minimumAverageDifference(List<int> nums) { |
| 112 | + // if there is only one element then itself would be the ans (index 0). |
| 113 | + if (nums.length == 1) return 0; |
| 114 | + |
| 115 | + // finding total sum of vector (using int int to prevent overflow) |
| 116 | + int sum = 0; |
| 117 | + for (int a in nums) { |
| 118 | + sum += a; |
| 119 | + } |
| 120 | + |
| 121 | + //initially left side sum will be zero and right side sum will be total sum |
| 122 | + int leftSum = 0; |
| 123 | + int rightSum = sum; |
| 124 | + |
| 125 | + // this will be used to keep track of minimum |
| 126 | + int avgDiff; |
| 127 | + int mini = -1; |
| 128 | + int minIndex = -1; |
| 129 | + int n = nums.length; |
| 130 | + |
| 131 | + for (int i = 0; i < n - 1; i++) { |
| 132 | + // increasing left sum and decreasing right sum |
| 133 | + leftSum += nums[i]; |
| 134 | + rightSum -= nums[i]; |
| 135 | + |
| 136 | + // finding avgDiff , (here (n-i-1) represents element count in right part, ans (i+1) in left part) |
| 137 | + avgDiff = ((leftSum ~/ (i + 1)) - (rightSum ~/ (n - i - 1))).abs(); |
| 138 | + |
| 139 | + //updating mini |
| 140 | + if (mini == -1) { |
| 141 | + mini = avgDiff; |
| 142 | + minIndex = i; |
| 143 | + } else if (mini > avgDiff) { |
| 144 | + mini = avgDiff; |
| 145 | + minIndex = i; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + // check for last element |
| 150 | + avgDiff = sum ~/ n; |
| 151 | + if (mini > avgDiff) { |
| 152 | + mini = avgDiff; |
| 153 | + minIndex = n - 1; |
| 154 | + } |
| 155 | + return minIndex; |
| 156 | + } |
| 157 | +} |
0 commit comments