Skip to content

Commit 71ac4c9

Browse files
committed
leetcode
1 parent 290fed9 commit 71ac4c9

File tree

4 files changed

+282
-0
lines changed

4 files changed

+282
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
func checkSubarraySum(nums []int, k int) bool {
4+
res := 0
5+
var m map[int]int
6+
m = make(map[int]int)
7+
for i := 0; i < len(nums); i++ {
8+
res = (res + nums[i]) % k
9+
if i > 0 && res == 0 {
10+
return true
11+
}
12+
if m[res] != 0 {
13+
if i+1-m[res] > 1 {
14+
return true
15+
}
16+
} else {
17+
m[res] = i + 1
18+
}
19+
}
20+
return false
21+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# 🔥 Continuous SubArray Sum 🔥 || 3 Solutions || Simple Fast and Easy || with Explanation
2+
3+
## Approach:-
4+
5+
- We want to check if there is any [L, R] such that `Sum(L, R) % k == 0`
6+
- Let's represent Sum(L, R) in terms of prefixSums.
7+
- `Sum(L, R) = Prefix[R] - Prefix[L] + Arr[L]`
8+
- We want to solve for, `Sum(L, R) % k = 0`
9+
`(Prefix[R] - Prefix[L] + Arr[L]) % k = 0`
10+
- LHS % k = 0 =>LHS should be a multiple of k
11+
- `Prefix[R] - Prefix[L] + Arr[L] = constant *k`
12+
- `Prefix[R] = Prefix[L] - Arr[L] + constant* k`
13+
- Taking % k both sides
14+
- `Prefix[R] % k = (Prefix[L] - Arr[L] + constant * k) % k`
15+
- `Prefix[R] % k = (Prefix[L] - Arr[L]) % k`
16+
- 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.
17+
- 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.
18+
19+
## Solution - 1 HashSet
20+
21+
```dart
22+
import 'dart:collection';
23+
24+
class Solution {
25+
// Runtime: 899 ms, faster than 100.00% of Dart online submissions for Continuous Subarray Sum.
26+
// Memory Usage: 204.9 MB, less than 100.00% of Dart online submissions for Continuous Subarray Sum.
27+
bool checkSubarraySum(List<int> nums, int k) {
28+
HashSet<int> modSet = HashSet();
29+
int currSum = 0, prevSum = 0;
30+
//when we add prevSum=0 in set it will actually check if currSum is divided by k
31+
for (int n in nums) {
32+
currSum += n;
33+
if (modSet.contains(currSum % k)) {
34+
return true;
35+
}
36+
currSum %= k;
37+
modSet.add(prevSum);
38+
prevSum = currSum;
39+
}
40+
return false;
41+
}
42+
}
43+
```
44+
45+
## Solution - 2 Brute Force
46+
47+
```dart
48+
class Solution {
49+
// Runtime: 696 ms, faster than 100.00% of Dart online submissions for Continuous Subarray Sum.
50+
// Memory Usage: 186.4 MB, less than 100.00% of Dart online submissions for Continuous Subarray Sum.
51+
bool checkSubarraySum(List<int> nums, int k) {
52+
for (int i = 1; i < nums.length; i++) {
53+
if (nums[i] == 0 && nums[i - 1] == 0) return true;
54+
}
55+
for (int i = 1; i < nums.length; i++) {
56+
nums[i] += nums[i - 1];
57+
if (nums[i] % k == 0) return true;
58+
int j = i;
59+
while (j > 1 && nums[i] > k) {
60+
if ((nums[i] - nums[j - 2]) % k == 0) {
61+
return true;
62+
}
63+
j--;
64+
}
65+
}
66+
return false;
67+
}
68+
}
69+
```
70+
71+
## Solution - 3
72+
73+
```dart
74+
import 'dart:collection';
75+
76+
class Solution {
77+
// Runtime: 668 ms, faster than 100.00% of Dart online submissions for Continuous Subarray Sum.
78+
// Memory Usage: 207.5 MB, less than 100.00% of Dart online submissions for Continuous Subarray Sum.
79+
bool checkSubarraySum(List<int> nums, int k) {
80+
if (nums.length < 2) return false;
81+
82+
// Map<remainder, index>
83+
HashMap<int, int> map = HashMap();
84+
85+
map[0] = -1; // Why? Find the answer below
86+
87+
int currSum = 0; // This would be our running sum
88+
89+
for (int i = 0; i < nums.length; i++) {
90+
currSum += nums[i];
91+
int rem = 0;
92+
93+
if (k != 0) rem = currSum % k; // k can't be 0 when we do a number % k
94+
95+
if (map.containsKey(rem)) {
96+
// if that remainder already exists
97+
if (i - map[rem]! > 1) {
98+
// Length/difference checking Step
99+
return true; // and if the diff between the indices of the same remainder > 1, we get our answer
100+
}
101+
}
102+
103+
map.putIfAbsent(
104+
rem,
105+
() =>
106+
i); // else we put that remainder along with its index in the map
107+
108+
// we don't do map.put(rem, i) because it'll overwrite the old index (value) for the same rem (key).
109+
// using a 'putIfAbsent' will create a new unique map which we want.
110+
}
111+
112+
return false;
113+
}
114+
}
115+
```

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
108108
- [Set Mismatch](SetMismatch/set_mismatch.dart)
109109
- [Maximum Length of a Concatenated String with Unique Characters](MaximumLengthOfAConcatenatedStringWithUniqueCharacters/maximum_length_of_a_concatenated_string_with_unique_characters.dart)
110110
- [Check If Two String Arrays are Equivalent](CheckIfTwoStringArraysAreEquivalent/check_if_two_string_arrays_are_equivalent.dart)
111+
- [Continuous SubArray Sum](ContinuousSubarraySum/continuous_subarray_sum.dart)
111112

112113
## Reach me via
113114

0 commit comments

Comments
 (0)