If there exists no subarray in the given sequence whose sum is divisible by ‘K’ then simply return ‘0’.
Suppose the given array is ‘ARR’ = { 5, 0, 2, 3, 1} and ‘K = 5’.
As we can see in below image, there are a total of 6 subarrays that have the total sum divisible by ‘K’
So we return the integer 6.
The first line of input contains an integer ‘T’ denoting the number of test cases.
The next ‘2*T’ lines represent the ‘T’ test cases.
The first line of each test case contains two space-separated integers the first integer ‘N’ will denote the number of elements in the array and the second integer denotes integer ‘K’.
The second line of each test case contains ‘N’ space-separated integer that is elements of the array.
For each test case, print an integer that is the count of all subarray that sum is divisible by ‘K’.
You are not required to print the output explicitly, it has already been taken care of. Just implement the function.
1 <= T <= 50
1 <= K,N <= 10^4
-10^9 <= ARR[i] <= 10^9
Time limit: 1 second
The basic idea is that try each and every possible subarray and find the sum of the current subarray and check if the current sum is divisible by ‘K’.
Let’s consider the sum of subarray ( ‘i’ to ‘j’ ) is divisible by ‘K’ and we can represent the sum of a subarray ( ‘i’ to ‘j’ ) = ( subarray sum (‘0’ to ‘j’ ) - subarray sum ( ‘0’ to ‘i-1’) ).
The Sum of any subarray can be written as ‘SUM = QUO * K + REM’, where ‘QUO’ is quotient and ‘REM’ is remainder.
Sum of subarray ( ‘i’ to ‘j’ ) = ( K * QUO2+ REM2 ) - ( K * QUO1 + REM2 ) = K * ( QUO2 - QUO1 ) +( REM2 - REM1 ).
We can see sum of subarray ( ‘i’ to ‘j’ ) will be divisible by ‘K’ if ‘REM2 - REM1 = 0’ or ‘REM1 == REM2’.
If sum of subarray from ‘i’ to ‘j’ is divisible by ‘K’ then ( ARR[0] + ARR[1] …… + ARR[i] ) % K == ( ARR [0] + ARR[1] + ARR[ 2] + ……. + ARR[j]) % K must be follow.
Let’s consider the sum of subarray ( ‘i’ to ‘j’ ) is divisible by ‘K’ and we can represent the sum of a subarray ( ‘i’ to ‘j’ ) = ( subarray sum (‘0’ to ‘j’ ) - subarray sum ( ‘0’ to ‘i-1’) ).
The Sum of any subarray can be written as ‘SUM = QUO * K + REM’, where 'QUO' is quotient and ‘REM’ is remainder.
Sum of subarray ( ‘i’ to ‘j’ ) = ( K * QUO2 + REM2 ) - ( K * QUO1 + REM2 ) = K * ( QUO2 - QUO1 ) +( REM2 - REM1 ).
We can see the sum of subarray ( ‘i’ to ‘j’ ) will be divisible by K, if ‘REM2 - REM1 = 0’ or ‘REM1 == REM2’.
From the above intuition, we can say that if we are currently at position ‘i’ and the current remainder is ‘X’ and ‘X’ remainder is already present at index ‘IDX’ then subarray ( ‘IDX + 1’ to ‘i’ ) has to sum is divisible by ‘K’.
Suppose given 'K' is '7'.
Observation - the remainder is ‘3’ at the index '4'. The figure shows that the remainder ‘3’ is already present at the index '2' so that sum of elements from indices '2+1' to '4' is divisible by '7'.
Implementation-
Merge Two Sorted Arrays Without Extra Space
Merge Two Sorted Arrays Without Extra Space
Ninja And The Strictly Increasing Array
Maximum GCD
Negative To The End
Find Duplicate in Array