|
| 1 | +/* |
| 2 | +
|
| 3 | + -* Sum of Even Numbers After Queries *- |
| 4 | +
|
| 5 | +You are given an integer array nums and an array queries where queries[i] = [vali, indexi]. |
| 6 | +
|
| 7 | +For each query i, first, apply nums[indexi] = nums[indexi] + vali, then print the sum of the even values of nums. |
| 8 | +
|
| 9 | +Return an integer array answer where answer[i] is the answer to the ith query. |
| 10 | +
|
| 11 | +
|
| 12 | +
|
| 13 | +Example 1: |
| 14 | +
|
| 15 | +Input: nums = [1,2,3,4], queries = [[1,0],[-3,1],[-4,0],[2,3]] |
| 16 | +Output: [8,6,2,4] |
| 17 | +Explanation: At the beginning, the array is [1,2,3,4]. |
| 18 | +After adding 1 to nums[0], the array is [2,2,3,4], and the sum of even values is 2 + 2 + 4 = 8. |
| 19 | +After adding -3 to nums[1], the array is [2,-1,3,4], and the sum of even values is 2 + 4 = 6. |
| 20 | +After adding -4 to nums[0], the array is [-2,-1,3,4], and the sum of even values is -2 + 4 = 2. |
| 21 | +After adding 2 to nums[3], the array is [-2,-1,3,6], and the sum of even values is -2 + 6 = 4. |
| 22 | +Example 2: |
| 23 | +
|
| 24 | +Input: nums = [1], queries = [[4,0]] |
| 25 | +Output: [0] |
| 26 | +
|
| 27 | +
|
| 28 | +Constraints: |
| 29 | +
|
| 30 | +1 <= nums.length <= 104 |
| 31 | +-104 <= nums[i] <= 104 |
| 32 | +1 <= queries.length <= 104 |
| 33 | +-104 <= vali <= 104 |
| 34 | +0 <= indexi < nums.length |
| 35 | +
|
| 36 | +
|
| 37 | +
|
| 38 | +
|
| 39 | +*/ |
| 40 | + |
| 41 | +// O(n) |
| 42 | +class A { |
| 43 | +// Runtime: 608 ms, faster than 100.00% of Dart online submissions for Sum of Even Numbers After Queries. |
| 44 | +// Memory Usage: 178.7 MB, less than 100.00% of Dart online submissions for Sum of Even Numbers After Queries. |
| 45 | + List<int> sumEvenAfterQueries(List<int> nums, List<List<int>> queries) { |
| 46 | + List<int> ret = []; |
| 47 | + int sum = 0; |
| 48 | + |
| 49 | + for (var query in queries) { |
| 50 | + if (ret.length == 0) { |
| 51 | + nums[query[1]] = query[0] + nums[query[1]]; |
| 52 | + // calculate the sum of even nums |
| 53 | + for (int a in nums) if (a % 2 == 0) sum += a; |
| 54 | + } else { |
| 55 | + if (nums[query[1]] % 2 == 0) sum -= nums[query[1]]; |
| 56 | + nums[query[1]] = query[0] + nums[query[1]]; |
| 57 | + if (nums[query[1]] % 2 == 0) sum += nums[query[1]]; |
| 58 | + } |
| 59 | + |
| 60 | + ret.add(sum); |
| 61 | + } |
| 62 | + |
| 63 | + return ret; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// O(m + n), Traverse complexity |
| 68 | +class B { |
| 69 | +// Runtime: 672 ms, faster than 100.00% of Dart online submissions for Sum of Even Numbers After Queries. |
| 70 | +// Memory Usage: 177.3 MB, less than 100.00% of Dart online submissions for Sum of Even Numbers After Queries. |
| 71 | + List<int> sumEvenAfterQueries(List<int> nums, List<List<int>> queries) { |
| 72 | + int sumEven = 0; |
| 73 | + // List<int> ret = [queries.length]; |
| 74 | + List<int> ret = List.filled(queries.length, 0); |
| 75 | + for (int i = 0; i < nums.length; i++) { |
| 76 | + if (nums[i] % 2 == 0) { |
| 77 | + sumEven += nums[i]; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + //queries |
| 82 | + for (int i = 0; i < queries.length; i++) { |
| 83 | + int prev = nums[queries[i][1]]; |
| 84 | + nums[queries[i][1]] += queries[i][0]; |
| 85 | + // case 1: prev was even, now again it is even |
| 86 | + if (prev % 2 == 0 && nums[queries[i][1]] % 2 == 0) { |
| 87 | + sumEven -= prev; |
| 88 | + sumEven += nums[queries[i][1]]; |
| 89 | + } |
| 90 | + // case 2: prev was even, now odd |
| 91 | + else if (prev % 2 == 0 && nums[queries[i][1]] % 2 != 0) { |
| 92 | + sumEven -= prev; |
| 93 | + } |
| 94 | + // case 3: prev odd, now even |
| 95 | + else if (prev % 2 != 0 && nums[queries[i][1]] % 2 == 0) { |
| 96 | + sumEven += nums[queries[i][1]]; |
| 97 | + } |
| 98 | + // case 4: prev odd, now odd |
| 99 | + |
| 100 | + ret[i] = sumEven; |
| 101 | + } |
| 102 | + return ret; |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +class C { |
| 107 | + /* |
| 108 | +
|
| 109 | +Intuition: |
| 110 | +
|
| 111 | +For each query, we need to check if the number at the index is even or odd. |
| 112 | +We need to apply nums[index] = nums[index] + val, then print the sum of all even numbers. |
| 113 | +So, we can basically pre-compute the sum of all even numbers in the array. |
| 114 | +This will allow us to quickly update the sum when we add or remove an even number. |
| 115 | +Approach: |
| 116 | +
|
| 117 | +Find the sum of all even numbers in the array |
| 118 | +For each query, add the value to the index |
| 119 | +If the new value is even, add it to the sum |
| 120 | +If the old value was even, subtract it from the sum |
| 121 | +Add the sum to the answer |
| 122 | +
|
| 123 | +
|
| 124 | +*/ |
| 125 | +// Time Complexity: O(n + q), where n is the length of nums and q is the length of queries. |
| 126 | +// Space Complexity: O(q) , where q is the length of queries |
| 127 | + List<int> sumEvenAfterQueries(List<int> nums, List<List<int>> queries) { |
| 128 | + int sum = 0; |
| 129 | + for (int i = 0; i < nums.length; i++) { |
| 130 | + if (nums[i] % 2 == 0) { |
| 131 | + sum += nums[i]; |
| 132 | + } |
| 133 | + } |
| 134 | + List<int> ans = []; |
| 135 | + for (int i = 0; i < queries.length; i++) { |
| 136 | + int val = queries[i][0]; |
| 137 | + int index = queries[i][1]; |
| 138 | + if (nums[index] % 2 == 0) { |
| 139 | + sum -= nums[index]; |
| 140 | + } |
| 141 | + nums[index] += val; |
| 142 | + if (nums[index] % 2 == 0) { |
| 143 | + sum += nums[index]; |
| 144 | + } |
| 145 | + ans.add(sum); |
| 146 | + } |
| 147 | + return ans; |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +// class D { |
| 152 | +// List<int> sumEvenAfterQueries(List<int> nums, List<List<int>> queries) { |
| 153 | +// return queries.map((query) { |
| 154 | +// nums[query[1]] += query[0]; |
| 155 | +// return nums.reduce((acc, cur) => cur % 2 == 0 ? acc : acc + cur, 0); |
| 156 | +// }).toList(); |
| 157 | +// } |
| 158 | +// } |
| 159 | + |
| 160 | +class E { |
| 161 | +/* |
| 162 | +
|
| 163 | +Intuition - |
| 164 | +
|
| 165 | +First, pre-compute the sum of all even numbers. |
| 166 | +Now for each query, do the required operation and see if it effects the sum in any way. |
| 167 | +If it does, then change the sum accordingly and store it in ans. |
| 168 | +Algorithm - |
| 169 | +
|
| 170 | +Do the sum of all even numbers in nums array |
| 171 | +Now for each query, there can be 3 possibilities. |
| 172 | +Previously nums[index] was even, but now it is odd - So we need to subtract the previous value from sum. |
| 173 | +Previously nums[index] was odd, but now it is even - So we need to add current value to sum. |
| 174 | +Previously nums[index] was even and after sum operation, it is still even - In this case, we need to substract the previous value and add the current value to sum. |
| 175 | +Return ans array |
| 176 | +
|
| 177 | +
|
| 178 | +
|
| 179 | +Complexity Analysis - |
| 180 | +Time - O(N+M) where N is the length of nums array and M is the length of queries matrix. |
| 181 | +Space - O(M) for resultant array. |
| 182 | +
|
| 183 | + */ |
| 184 | +// Runtime: 437 ms, faster than 100.00% of Dart online submissions for Sum of Even Numbers After Queries. |
| 185 | +// Memory Usage: 157.6 MB, less than 100.00% of Dart online submissions for Sum of Even Numbers After Queries. |
| 186 | + List<int> sumEvenAfterQueries(List<int> nums, List<List<int>> queries) { |
| 187 | + int sum = 0; |
| 188 | + for (int n in nums) { |
| 189 | + if (n % 2 == 0) sum += n; |
| 190 | + } |
| 191 | + |
| 192 | + List<int> ans = List.filled(queries.length, 0); |
| 193 | + int i = 0; |
| 194 | + for (var query in queries) { |
| 195 | + int val = query[0]; |
| 196 | + int index = query[1]; |
| 197 | + |
| 198 | + bool previouslyEven = nums[index] % 2 == 0; |
| 199 | + int prev = nums[index]; |
| 200 | + nums[index] += val; |
| 201 | + bool currentlyEven = nums[index] % 2 == 0; |
| 202 | + int curr = nums[index]; |
| 203 | + |
| 204 | + if (previouslyEven && currentlyEven) { |
| 205 | + sum -= prev; |
| 206 | + sum += curr; |
| 207 | + } else if (!previouslyEven && currentlyEven) { |
| 208 | + sum += curr; |
| 209 | + } else if (previouslyEven && !currentlyEven) { |
| 210 | + sum -= prev; |
| 211 | + } |
| 212 | + |
| 213 | + ans[i++] = sum; |
| 214 | + } |
| 215 | + |
| 216 | + return ans; |
| 217 | + } |
| 218 | +} |
0 commit comments