 New update is available. Click here to update.

# Sub-array Sums Divisible By K

Contributed by
Sakshi Bansal
Last Updated: 23 Feb, 2023
Medium 0/80
Avg time to solve 25 mins
Success Rate 75 % Share 4 upvotes

## Problem Statement

#### 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.

##### 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.
``````
Detailed explanation ( Input/output format, Notes, Images ) ##### 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
``````
##### Sample Input 1 :
``````2
5 5
5 -5 0 -1 2
1 4
3
``````
##### Sample Output 1 :
``````6
0
``````
##### Explanation of Sample Input 1 :
``````Test Case 1: Among all the possible subarrays of the given array, there are six subarrays whose sum is divisible by 5.
[ 5 ]
[ 5, -5]
[ 5, -5, 0 ]
[ -5, 0 ]
[ -5 ]
[ 0 ]

Test Case 2: The only subarray  is not divisible by 4.
``````
##### Sample Input 2 :
``````2
6 5
4 5 0 -2 -3 1
7 3
6 7 1 -2 3 4 9
``````
##### Sample Output 2 :
``````7
9
``````  Auto Console