|
| 1 | +/* |
| 2 | +
|
| 3 | +
|
| 4 | +-* Continuous SubArray Sum *- |
| 5 | +
|
| 6 | +
|
| 7 | +
|
| 8 | +Given an integer array nums and an integer k, return true if nums has a continuous sub-array of size at least two whose elements sum up to a multiple of k, or false otherwise. |
| 9 | +
|
| 10 | +An integer x is a multiple of k if there exists an integer n such that x = n * k. 0 is always a multiple of k. |
| 11 | +
|
| 12 | +
|
| 13 | +
|
| 14 | +Example 1: |
| 15 | +
|
| 16 | +Input: nums = [23,2,4,6,7], k = 6 |
| 17 | +Output: true |
| 18 | +Explanation: [2, 4] is a continuous sub-array of size 2 whose elements sum up to 6. |
| 19 | +Example 2: |
| 20 | +
|
| 21 | +Input: nums = [23,2,6,4,7], k = 6 |
| 22 | +Output: true |
| 23 | +Explanation: [23, 2, 6, 4, 7] is an continuous sub-array of size 5 whose elements sum up to 42. |
| 24 | +42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer. |
| 25 | +Example 3: |
| 26 | +
|
| 27 | +Input: nums = [23,2,6,4,7], k = 13 |
| 28 | +Output: false |
| 29 | +
|
| 30 | +
|
| 31 | +Constraints: |
| 32 | +
|
| 33 | +1 <= nums.length <= 105 |
| 34 | +0 <= nums[i] <= 109 |
| 35 | +0 <= sum(nums[i]) <= 231 - 1 |
| 36 | +1 <= k <= 231 - 1 |
| 37 | +
|
| 38 | +*/ |
| 39 | + |
| 40 | +import 'dart:collection'; |
| 41 | + |
| 42 | +/* |
| 43 | +
|
| 44 | +Explanation 📝 |
| 45 | +We want to check if there is any [L, R] such that Sum(L, R) % k == 0 |
| 46 | +Let's represent Sum(L, R) in terms of prefixSums. |
| 47 | +Sum(L, R) = Prefix[R] - Prefix[L] + Arr[L] |
| 48 | +We want to solve for, Sum(L, R) % k = 0 |
| 49 | +(Prefix[R] - Prefix[L] + Arr[L]) % k = 0 |
| 50 | +LHS % k = 0 =>LHS should be a multiple of k |
| 51 | +Prefix[R] - Prefix[L] + Arr[L] = constant * k |
| 52 | +Prefix[R] = Prefix[L] - Arr[L] + constant * k |
| 53 | +Taking % k both sides |
| 54 | +Prefix[R] % k = (Prefix[L] - Arr[L] + constant * k) % k |
| 55 | +Prefix[R] % k = (Prefix[L] - Arr[L]) % k |
| 56 | +So basically, for every R, we can check if there is any L such that(Prefix[L] - Arr[L]) % k equal to Prefix[R] % k, which can be done easily maintaining a Hashset for previously visited values. |
| 57 | +But, how will we handle Subarray length >= 2 case? It's very easy to do so, We will only check for L values < R which will make sure subarray length is at least 2. |
| 58 | +
|
| 59 | +
|
| 60 | +
|
| 61 | +Time Complexity: O(N) |
| 62 | +Space Complexity: O(Min(N, K)) |
| 63 | +*/ |
| 64 | + |
| 65 | +class A { |
| 66 | + // Runtime: 899 ms, faster than 100.00% of Dart online submissions for Continuous Subarray Sum. |
| 67 | +// Memory Usage: 204.9 MB, less than 100.00% of Dart online submissions for Continuous Subarray Sum. |
| 68 | + bool checkSubarraySum(List<int> nums, int k) { |
| 69 | + HashSet<int> modSet = HashSet(); |
| 70 | + int currSum = 0, prevSum = 0; |
| 71 | + //when we add prevSum=0 in set it will actually check if currSum is divided by k |
| 72 | + for (int n in nums) { |
| 73 | + currSum += n; |
| 74 | + if (modSet.contains(currSum % k)) { |
| 75 | + return true; |
| 76 | + } |
| 77 | + currSum %= k; |
| 78 | + modSet.add(prevSum); |
| 79 | + prevSum = currSum; |
| 80 | + } |
| 81 | + return false; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +class B { |
| 86 | +// Runtime: 696 ms, faster than 100.00% of Dart online submissions for Continuous Subarray Sum. |
| 87 | +// Memory Usage: 186.4 MB, less than 100.00% of Dart online submissions for Continuous Subarray Sum. |
| 88 | + bool checkSubarraySum(List<int> nums, int k) { |
| 89 | + for (int i = 1; i < nums.length; i++) { |
| 90 | + if (nums[i] == 0 && nums[i - 1] == 0) return true; |
| 91 | + } |
| 92 | + for (int i = 1; i < nums.length; i++) { |
| 93 | + nums[i] += nums[i - 1]; |
| 94 | + if (nums[i] % k == 0) return true; |
| 95 | + int j = i; |
| 96 | + while (j > 1 && nums[i] > k) { |
| 97 | + if ((nums[i] - nums[j - 2]) % k == 0) { |
| 98 | + return true; |
| 99 | + } |
| 100 | + j--; |
| 101 | + } |
| 102 | + } |
| 103 | + return false; |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +class C { |
| 108 | + // Runtime: 668 ms, faster than 100.00% of Dart online submissions for Continuous Subarray Sum. |
| 109 | +// Memory Usage: 207.5 MB, less than 100.00% of Dart online submissions for Continuous Subarray Sum. |
| 110 | + bool checkSubarraySum(List<int> nums, int k) { |
| 111 | + if (nums.length < 2) return false; |
| 112 | + |
| 113 | + // Map<remainder, index> |
| 114 | + HashMap<int, int> map = HashMap(); |
| 115 | + |
| 116 | + map[0] = -1; // Why? Find the answer below |
| 117 | + |
| 118 | + int currSum = 0; // This would be our running sum |
| 119 | + |
| 120 | + for (int i = 0; i < nums.length; i++) { |
| 121 | + currSum += nums[i]; |
| 122 | + int rem = 0; |
| 123 | + |
| 124 | + if (k != 0) rem = currSum % k; // k can't be 0 when we do a number % k |
| 125 | + |
| 126 | + if (map.containsKey(rem)) { |
| 127 | + // map.keys.firstWhere((element) => map[element] == rem) // if that remainder already exists |
| 128 | + if (i - map[rem]! > 1) { |
| 129 | + // Length/difference checking Step |
| 130 | + return true; // and if the diff between the indices of the same remainder > 1, we get our answer |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + map.putIfAbsent( |
| 135 | + rem, |
| 136 | + () => |
| 137 | + i); // else we put that remainder along with its index in the map |
| 138 | + |
| 139 | + // we don't do map.put(rem, i) because it'll overwrite the old index (value) for the same rem (key). |
| 140 | + // using a 'putIfAbsent' will create a new unique map which we want. |
| 141 | + } |
| 142 | + |
| 143 | + return false; |
| 144 | + } |
| 145 | +} |
0 commit comments