Skip to content

Commit fd3a102

Browse files
authored
Merge pull request #979 from parikhitritgithub/dev
Adding the Solution of a problem 523
2 parents 2600b45 + c18c927 commit fd3a102

File tree

1 file changed

+211
-0
lines changed

1 file changed

+211
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
---
2+
id: continuous-subarray-sum
3+
title: continuous-subarray-sum
4+
sidebar_label: 0523 continuous subarray sum
5+
tags:
6+
- prefix sum + hashmap
7+
- LeetCode
8+
- Java
9+
- Python
10+
- C++
11+
description: This is a solution to the Continuous Subarray Sum problem on LeetCode
12+
---
13+
14+
## Problem Description
15+
16+
Given an integer array `nums` and an integer `k` return `true` if `nums` has a good subarray or `false` otherwise A good subarray is a subarray where
17+
18+
- Its length is at least two and
19+
- The sum of the elements of the subarray is a multiple of `k`
20+
21+
Note that:
22+
- A subarray is a contiguous part of the array
23+
- 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`
24+
25+
### Examples
26+
27+
**Example 1:**
28+
29+
30+
31+
## Problem Description
32+
33+
Given an integer array nums and an integer k return true if nums has a good subarray or false otherwise
34+
A good subarray is a subarray where:
35+
36+
- its length is at least two and
37+
- the sum of the elements of the subarray is a multiple of k
38+
Note that:
39+
- A subarray is a contiguous part of the array
40+
- 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
41+
42+
### Examples
43+
44+
**Example 1:**
45+
46+
```
47+
48+
Input: nums : [23,2,4,6,7], k : 6
49+
Output: true
50+
Explanation: [2, 4] is a continuous subarray of size 2 whose elements sum up to 6
51+
52+
```
53+
54+
**Example 2:**
55+
56+
57+
```
58+
Input: root : nums : [23,2,6,4,7], k : 6
59+
Output: true
60+
Explanation: [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42
61+
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer
62+
```
63+
64+
**Example 3:**
65+
66+
67+
```
68+
Input: nums : [23,2,6,4,7], k : 13
69+
Output: false
70+
```
71+
72+
73+
### Constraints
74+
75+
- $1 \leq \text{nums.length} \leq 105$
76+
77+
78+
---
79+
80+
## Solution for Continuous Subarray Sum Problem
81+
82+
### Intuition
83+
This is a prefix sum's problem Due to LC
84+
85+
A good subarray is a subarray where:
86+
its length is at least two and
87+
the sum of the elements of the subarray is a multiple of k
88+
89+
modulo k there are 0,1,...k-1 totally k possible for prefix sum (mod k)
90+
For this constraint $1 \leq \text{nums.length} \leq 105$ an $O(n^2)$ solution may lead to TLE
91+
92+
93+
A hash map with care on prefix sum mod k to use is however a tip The array version is used when `k<n` combining the hash map version that will be the faster than other solutions
94+
95+
Thanks to the comments of @Sergei proposing to use unordered_set a combination of bitset with unordered_set is implemented which outperforms all other solutions with the elapsed time 86ms
96+
97+
98+
### Approach
99+
100+
101+
- First try uses array version for` mod_k(k)`
102+
- 2nd approach uses `unordered_map<int, vector<int>>` instead of an array which is accepted by LC
103+
- Since the computation uses `mod_k[prefix].front()`, a simple hash table `unordered_map<int, int> mod_k`is sufficient for this need
104+
- An acceptable version using array version when $k<n$ otherwise hash map which is a combination of both different data structures
105+
- Let prefix denote the current prefix sum modulo k The very crucial part is the following:
106+
107+
108+
109+
110+
#### Code in Different Languages
111+
112+
<Tabs>
113+
<TabItem value="Python" label="Python">
114+
<SolutionAuthor name="@parikhitkurmi"/>
115+
116+
```python
117+
//python
118+
119+
class Solution:
120+
def checkSubarraySum(self, nums: List[int], k: int) -> bool:
121+
n=len(nums)
122+
if n<2: return False
123+
mod_k={}
124+
prefix=0
125+
mod_k[0]=-1
126+
for i, x in enumerate(nums):
127+
prefix+=x
128+
prefix%=k
129+
if prefix in mod_k:
130+
if i>mod_k[prefix]+1:
131+
return True
132+
else:
133+
mod_k[prefix]=i
134+
return False
135+
```
136+
</TabItem>
137+
<TabItem value="Java" label="Java">
138+
<SolutionAuthor name="@parikhitkurmi"/>
139+
140+
```java
141+
//java
142+
143+
class Solution {
144+
public boolean checkSubarraySum(int[] nums, int k) {
145+
int n = nums.length, prefSum = 0;
146+
Map<Integer, Integer> firstOcc = new HashMap<>();
147+
firstOcc.put(0, 0);
148+
149+
for (int i = 0; i < n; i++) {
150+
prefSum = (prefSum + nums[i]) % k;
151+
if (firstOcc.containsKey(prefSum)) {
152+
if (i + 1 - firstOcc.get(prefSum) >= 2) {
153+
return true;
154+
}
155+
} else {
156+
firstOcc.put(prefSum, i + 1);
157+
}
158+
}
159+
160+
return false;
161+
}
162+
}
163+
164+
```
165+
</TabItem>
166+
<TabItem value="C++" label="C++">
167+
<SolutionAuthor name="@parikhitkurmi"/>
168+
169+
```cpp
170+
//cpp
171+
172+
class Solution {
173+
public:
174+
bool checkSubarraySum(vector<int>& nums, int k) {
175+
map<int , int> mp ;
176+
mp[0] = 0 ;
177+
int sum = 0 ;
178+
for(int i = 0 ; i< nums.size() ; i++ ) {
179+
sum += nums[i] ;
180+
int rem = sum%k ;
181+
182+
if(mp.find(rem)==mp.end()){
183+
mp[rem] = i+1 ;
184+
185+
}else if (mp[rem] < i ) {
186+
return true ;
187+
188+
}
189+
190+
191+
}
192+
return false ;
193+
194+
}
195+
};
196+
197+
```
198+
199+
</TabItem>
200+
</Tabs>
201+
202+
203+
204+
205+
206+
## References
207+
208+
- **LeetCode Problem:** [Continuous Subarray Sum](https://leetcode.com/problems/continuous-subarray-sum/)
209+
- **Solution Link:** [Continuous Subarray Sum](https://leetcode.com/problems/continuous-subarray-sum/submissions/1281964300/)
210+
- **Authors GeeksforGeeks Profile:** [parikhit kurmi](https://www.geeksforgeeks.org/user/sololeveler673/)
211+
- **Authors Leetcode:** [parikhit kurmi](https://leetcode.com/u/parikhitkurmi14/)

0 commit comments

Comments
 (0)