Skip to content
Decoder

Continuous Subarray Sum

Medium

Problem

Given a list of non-negative integers nums and a positive integer k, return true if there is a contiguous subarray of length at least two whose sum is a multiple of `k`. Zero counts as a multiple of every k.

Example 1

Input
nums = [23, 2, 4, 6, 7], k = 6
Output
true
Why
[2, 4] sums to 6

Example 2

Input
nums = [23, 2, 6, 4, 7], k = 6
Output
true
Why
the whole list sums to 42

Example 3

Input
nums = [23, 2, 6, 4, 7], k = 13
Output
false

Constraints

  • 1 <= nums.length <= 10^5
  • 0 <= nums[i] <= 10^9
  • 0 <= sum(nums) <= 2^31 - 1
  • 1 <= k <= 2^31 - 1

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.