Subarray Sum Divisible by K

Given an array of integers and an integer K, find the number of subarrays whose sums are divisible by K.

Input: nums = [3,1,2,5,1], k = 3

Output: 6

Explanation: The six subarrays are [3], [3,1,2], [1,2], [5,1], [3,1,2,5,1], [1,2,5,1]

Constraints:

  • 1 <= nums.length <= 3 * 10^4
  • -10^4 <= nums[i] <= 10^4
  • 2 <= k <= 10^4

Try it yourself

Invest in Yourself
Your new job is waiting. 83% of people that complete the program get a job offer. Unlock unlimited access to all content and features.
Go Pro