BlackFridaySale

Join the waitlist for an extra exclusive deal

Extra savings end in
00Days
12Hrs
53Mins
01Secs
Limited spots only

307. Count of Subarrays with Sum Divisible by K

Given an array of integers, nums, and an integer k, the task is to find the total number of subarrays whose sum is divisible by k. A subarray is a contiguous portion of the array.

Example 1:

Input: nums = [3, 1, 4, 1], k = 3

Output: 3

Explanation: The subarrays whose sum is divisible by 3 are: [3], [1, 4, 1], [3, 1, 4, 1].

Example 2:

Input: nums = [5, 10, -5, 20], k = 7

Output: 0

Explanation: There is no subarray whose sum is divisible by 7.

Now Your Turn!

Pick the correct output for the given input

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

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= nums.length <= 105
  • -104 <= nums[i] <= 104
  • 2 <= k <= 104

Fun Facts

0
class Solution{
public:
int subarraySumDivisbleByK(vector<int> &nums, int k){
}
};
Test Case

Input:

K
Nums