#974

Subarray Sums Divisible by K

specialist · 770 · lc medium +31 · verified · 56.1% accepted · 7,835 likes · top 50%

Description

Given integer array nums and positive integer k, count how many non-empty contiguous subarrays have a sum that is a multiple of k.

Example 1:

Input: nums = [4,5,0,-2,-3,1], k = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by k = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]

Example 2:

Input: nums = [5], k = 9
Output: 0

Code

1
2
3