523. Continuous Subarray Sum - Detailed Explanation
Problem Statement
Given an integer array nums and an integer k, determine if there exists a continuous subarray of size at least two whose sum is a multiple of k (i.e. sum = m·k for some integer m). Return true if such a subarray exists, otherwise return false.
Examples
-
Input
nums = [23, 2, 4, 6, 7], k = 6
Output
true
Explanation
The subarray [2, 4] sums to 6, which is 1·6 -
Input
nums = [23, 2, 6, 4, 7], k = 6
Output
true
Explanation
[23, 2, 6, 4, 7] sums to 42 = 7·6 -
Input
nums = [23, 2, 6, 4, 7], k = 13
Output
false
Constraints
- 1 ≤ nums.length ≤ 10⁵
- -10⁹ ≤ nums[i] ≤ 10⁹
- 0 ≤ k ≤ 10⁹
Hints
- A brute‑force scan over all subarrays is O(n²) and will time out for n up to 10⁵.
- Use prefix sums and the fact that
to detect a valid subarray in O(n) time.if (sum[j] - sum[i]) % k == 0 ⇔ sum[j] % k == sum[i] % k
Approaches
Brute‑Force Subarray Check
Explanation
- Compute prefix sum array
Sof length n+1 whereS[0]=0andS[i]=sum(nums[0..i-1]). - For every pair (
i,j) withj - i ≥ 2, check if(S[j] - S[i]) % k == 0. - If any pair satisfies this, return
true.
Step‑by‑Step for [23,2,4,6,7], k=6
S = [0,23,25,29,35,42]
Check i=0,j=2: (S[2]-S[0])=25 → 25%6=1 ✗
i=0,j=3: 29→29%6=5 ✗
i=0,j=4: 35→35%6=5 ✗
i=0,j=5: 42→42%6=0 ✓ return true
Complexity Analysis
- Time O(n²) — double loop over i and j
- Space O(n) — prefix sums
Prefix‑Sum Modulo with HashMap
Explanation
- Maintain a running sum
sumand a mapmodIndexfromsum % kto the earliest index where this mod appeared. - Initialize
modIndex.put(0, -1)so that ifsum % k == 0at index ≥1, subarray from 0…i is valid. - Iterate
ifrom 0 to n-1:- Update
sum += nums[i]. - Compute
mod = k == 0 ? sum : sum % k. - If
modis already inmodIndexandi - modIndex.get(mod) ≥ 2, return true. - Otherwise, if
modnot seen before, recordmodIndex.put(mod, i).
- Update
- At end, return false.
Handling k = 0
- If
k == 0, look only for a subarray of length ≥2 with sum exactly 0. In that case, you can track running sum and check if it repeats (or track two consecutive zeros).
Step‑by‑Step for [23,2,4,6,7], k=6
modIndex = {0 → -1}
i=0: sum=23, mod=23%6=5, store 5→0
i=1: sum=25, mod=25%6=1, store 1→1
i=2: sum=29, mod=29%6=5, seen at index 0, length = 2-0 = 2 ≥2 → return true
Complexity Analysis
- Time O(n) — one pass with O(1) map operations
- Space O(min(n, k)) — map of seen mods
Python Code
Python3
Python3
. . . .
Java Code
Java
Java
. . . .
Common Mistakes
Ignoring k = 0
For k = 0, you must detect sum exactly 0 in a subarray of length ≥2.
Overwriting earliest index
Store only the first occurrence of each mod to maximize subarray length.
Off‑by‑one on length check
Ensure you require at least two elements: i - prevIndex ≥ 2.
Edge Cases
- Single element array (n=1) → always false
- All zeros with k > 0 → true if at least two zeros
- k = 1 → any subarray of length ≥2 sums to a multiple of 1 → true
Alternative Variations
- Find a subarray of exact length
mwhose sum is a multiple of k - Detect any subarray (of any length) with sum exactly k
Related Problems
TAGS
leetcode
CONTRIBUTOR
Design Gurus Team
-
GET YOUR FREE
Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
825. Friends Of Appropriate Ages - Detailed Explanation
Learn to solve Leetcode 825. Friends Of Appropriate Ages with multiple approaches.
392. Is Subsequence - Detailed Explanation
Learn to solve Leetcode 392. Is Subsequence with multiple approaches.
2182. Construct String With Repeat Limit - Detailed Explanation
Learn to solve Leetcode 2182. Construct String With Repeat Limit with multiple approaches.
934. Shortest Bridge - Detailed Explanation
Learn to solve Leetcode 934. Shortest Bridge with multiple approaches.
546. Remove Boxes - Detailed Explanation
Learn to solve Leetcode 546. Remove Boxes with multiple approaches.
1752. Check if Array Is Sorted and Rotated - Detailed Explanation
Learn to solve Leetcode 1752. Check if Array Is Sorted and Rotated with multiple approaches.
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
4.6
(69,299 learners)
$197
New

Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
3.9
(1,107 learners)
$78
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
4
(26,683 learners)
$78
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.