Continuous Subarray Sum
Problem Statement
You are given an array nums of non-negative integers and a number k.
Return true if there is a contiguous subarray of length at least 2 whose sum is a multiple of k (0 counts as a multiple of every k). Otherwise return false.
An integer array nums (all values ≥ 0) and an integer k (k ≥ 1).
true if some subarray with 2 or more elements has a sum divisible by k, else false.
[2, 4] has length 2 and sum 6.
The whole array sums to 42 = 7 · 6.
No subarray of length 2 or more has a sum divisible by 13. Note [6] alone is divisible by 6 in the earlier example, but length 1 never counts.
- • 1 ≤ nums.length ≤ 10^5
- • 0 ≤ nums[i] ≤ 10^9
- • 0 ≤ sum(nums) ≤ 2^31 − 1
- • 1 ≤ k ≤ 2^31 − 1
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Fix each start, grow the end from start + 1, test sum % k
Store the earliest index per remainder; equal remainder ≥ 2 apart ⇒ true
For every start, begin with that one element, then add elements one by one from start + 1. Every sum we test now has at least 2 elements. Return true the first time a sum is a multiple of k.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Prefix Sum translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.