# Sub-array Sums Divisible By K

#### Given an array of integers of size ‘N’ and a positive integer ‘K’. Return the number of non-empty subarrays whose sum is divisible by K.

##### A subarray is a contiguous subset of an array.

##### For Example :

```
Consider an array of size four. The elements of the array are { -4, 5, 6, 1}.
The value of K is 4.
The subarrays whose sum is divisible by 4 are as follows:
[ -4 ]
[-4, 5, 6, 1]
[ 5, 6, 1]
Hence, there are three subarrays whose sum is divisible by 4.
```

##### Input Format :

```
The first line of input contains an integer T, the number of test cases.
The first line of every test case contains two space-separated integers ‘N’ and ‘K‘ denoting the size of the array and the positive integer K.
The second line of every test case contains ‘N’ space-separated integers.
```

##### Output Format :

```
For every test case, print the count of the subarrays whose sum is divisible by K.
The output of each test case is printed in a separate line.
```

##### Note :

```
You don’t have to print anything, it has already been taken care of. Just implement the function.
```

##### Constraints :

```
1 <= T <= 10
1 <= N <= 10^5
1 <= K <= 10^3
-10^3 <= data <= 10^3
Where ‘data’ denotes the value of the elements of the array.
Time Limit: 1 sec
```

##### Follow Up :

```
The O(N^2) solution is trivial, can you solve it in less than O(N^2) time?
```

The idea is very simple. We will generate all the possible subarrays of the given array. Now, we will find the sum of each subarray and keep track of the count of the subarrays whose sum is divisible by K.

**Algorithm**

- Initialize a variable ‘result’ that will store the total number of subarrays whose sum is divisible by K.
- Traverse the array from index 0 to N - 1 where the current index is denoted by i. Initialise a variable ‘sum’ with 0 that will store the sum of the current subarray.

We will use another for loop inside this loop, where the current index is denoted by j, to consider all the subarrays starting with the index i and ending at index j. - Inside this inner j loop, keep adding the elements to the ‘sum’.
- Outside the inner j loop, increment the ‘result’ if the sum of the current subarray is divisible by K.
- Return the ‘result’.

When we divide the sum of any subarray by K, the possible remainders we can get are 0, 1, 2,... K-1.

Consider two subarrays subArray(0, i) and subArray(0, j) where j > i.

Let’s say that when we divide the sum of the subarray(0, i) by K, we get the remainder ‘rem’.

Also, when we divide the sum of the subarray(0, j) by K, we get the same remainder ‘rem’.

It means that sum(subArray(0, i)) % K == sum(subArray(0, j)) % K.

Now, we can conclude that the sum of subarray(i + 1, j) leaves 0 as a remainder when divided by K. It means that the sum of the subarray(i+1, j) is completely divisible by K.

Thus, we will use an auxiliary array to store the remainders obtained by dividing the sum of subarrays. So, for current index j, we need to find out how many indexes i (i < j) exist that have the same mod of K.

For example, consider an array [ 2, 3, 4, 5, 6, 7] and K = 2.

Sum of the subarray [2, 3] = 5 and rem is 5%2 = 1.

Sum of the subarray [2, 3, 4] = 9 and rem is 9%2 = 1.

It means that the subarray [2, 3, 4] - subarray[2, 3] leaves a remainder of 0 when divided by 2.

As we can see, 4 is completely divisible by 2.

Algorithm

** **

- Create an array of size K as storeMod[K]. This array will store the count of each remainder we will get after dividing cumulative sum till any index in the given array.
- Initialise a variable ‘result’ by 0.
- Now, we will traverse the array and keep calculating cumulative sum and simultaneously take it’s Mod with K. Suppose the remainder is ‘rem’.
- For every remainder we get, we will check its count in storeMod array and add it to our result as shown here:

result += storeMod[rem] - Now we will increment the count of the ‘rem’ in storeMod array as:

storeMod[rem] += 1 - The variable ‘result’ will be the number of all possible subarrays divisible by K. We will simply return the ‘result’.