Skip to content

Commit ad71ea3

Browse files
medium: 523. Continuous Subarray Sum
1 parent 86c4e22 commit ad71ea3

2 files changed

Lines changed: 101 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {boolean}
5+
*/
6+
function checkSubarraySum(nums, k) {
7+
// Map to store the first occurrence of a remainder
8+
let remainderMap = new Map();
9+
remainderMap.set(0, -1); // This accounts for cases where subarray from beginning sums to multiple of k
10+
let prefixSum = 0;
11+
12+
for (let i = 0; i < nums.length; i++) {
13+
prefixSum += nums[i];
14+
15+
if (k !== 0) {
16+
prefixSum %= k;
17+
}
18+
19+
if (remainderMap.has(prefixSum)) {
20+
if (i - remainderMap.get(prefixSum) > 1) {
21+
return true;
22+
}
23+
} else {
24+
remainderMap.set(prefixSum, i);
25+
}
26+
}
27+
28+
return false;
29+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# [523. Continuous Subarray Sum](https://leetcode.com/problems/continuous-subarray-sum/post-solution)
2+
3+
---
4+
5+
title: "Continuous Subarray Sum Problem Solution"
6+
summary: "This document provides an efficient solution to the Check Subarray Sum problem using a hashmap to track remainders of the prefix sums."
7+
date: "2024-06-08"
8+
modifiedDate: "2024-06-08"
9+
tags: ["Algorithms", "JavaScript", "HashMap"]
10+
slug: "check-subarray-sum-problem-solution"
11+
12+
---
13+
14+
![image.png](https://assets.leetcode.com/users/images/de0bc58c-cdba-4714-a137-f6a8274fe509_1717808683.0167706.png)
15+
16+
# Intuition
17+
18+
To solve the problem of checking if a subarray sums to a multiple of `k`, we need to keep track of the cumulative sums (prefix sums) and their remainders when divided by `k`. By doing so, we can efficiently determine if there exists a subarray whose sum is a multiple of `k`.
19+
20+
# Approach
21+
22+
We use a hashmap to store the first occurrence of each remainder when the prefix sum is divided by `k`. This helps us to quickly check if the same remainder has appeared before, which would indicate that the subarray sum is a multiple of `k`. Here's a step-by-step approach:
23+
24+
1. Initialize a hashmap (`remainderMap`) to store remainders and their indices.
25+
2. Set the initial value of `prefixSum` to `0` and map the remainder `0` to index `-1` to handle cases where the subarray starts from the beginning.
26+
3. Iterate through the array, updating the `prefixSum` and calculating the current remainder when divided by `k`.
27+
4. If the current remainder has been seen before and the subarray length is greater than `1`, return `true`.
28+
5. Otherwise, store the current remainder and its index in the hashmap.
29+
6. If no valid subarray is found, return `false`.
30+
31+
# Complexity
32+
33+
- **Time complexity:** $$O(n)$$
34+
35+
- We traverse the array once, making the solution linear in time.
36+
37+
- **Space complexity:** $$O(min(n, k))$$
38+
- We store remainders in the hashmap, with the size limited to the number of unique remainders.
39+
40+
# Code
41+
42+
```javascript
43+
/**
44+
* @param {number[]} nums
45+
* @param {number} k
46+
* @return {boolean}
47+
*/
48+
function checkSubarraySum(nums, k) {
49+
// Map to store the first occurrence of a remainder
50+
let remainderMap = new Map();
51+
remainderMap.set(0, -1); // This accounts for cases where subarray from beginning sums to multiple of k
52+
let prefixSum = 0;
53+
54+
for (let i = 0; i < nums.length; i++) {
55+
prefixSum += nums[i];
56+
57+
if (k !== 0) {
58+
prefixSum %= k;
59+
}
60+
61+
if (remainderMap.has(prefixSum)) {
62+
if (i - remainderMap.get(prefixSum) > 1) {
63+
return true;
64+
}
65+
} else {
66+
remainderMap.set(prefixSum, i);
67+
}
68+
}
69+
70+
return false;
71+
}
72+
```

0 commit comments

Comments
 (0)