Skip to content

Continuous Subarray Sum

MediumArrays & Hashing
Decode it first. Clues, constraints, pattern and plan before any code.

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.

nums = [23, 2, 4, 6, 7],  k = 6    →  true   ([2, 4] sums to 6)
nums = [23, 2, 6, 4, 7],  k = 6    →  true   (the whole list sums to 42)
nums = [23, 2, 6, 4, 7],  k = 13   →  false

Constraints

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