You are given an array of positive and/or negative integers and a value K . The task is to find count of all sub-arrays whose sum is divisible by K?
Examples :
Input : arr[] = {4, 5, 0, -2, -3, 1},
K = 5
Output : 7
// there are 7 sub-arrays whose is divisible by K
// {4, 5, 0, -2, -3, 1}
// {5}
// {5, 0}
// {5, 0, -2, -3}
// {0}
// {0, -2, -3}
// {-2, -3}
A simple solution for this problem is to one by one calculate sum of all sub-arrays possible and check divisible by K. The time complexity for this approach will be O(n^2).
An efficient solution is based on below observation.
Let there be a subarray (i, j) whose sum is divisible by k
sum(i, j) = sum(0, j) - sum(0, i-1)
Sum for any subarray can be written as q*k + rem where q
is a quotient and rem is remainder
Thus,
sum(i, j) = (q1 * k + rem1) - (q2 * k + rem2)
sum(i, j) = (q1 - q2)k + rem1-rem2
We see, for sum(i, j) i.e. for sum of any subarray to be
divisible by k, the RHS should also be divisible by k.
(q1 - q2)k is obviously divisible by k, for (rem1-rem2) to
follow the same, rem1 = rem2 where
rem1 = Sum of subarray (0, j) % k
rem2 = Sum of subarray (0, i-1) % k
So if any sub-array sum from index i’th to j’th is divisible by k then we can saya[0]+…a[i-1] (mod k) = a[0]+…+a[j] (mod k)
The above explanation is provided by Ekta Goel.
So we need to find such a pair of indices (i, j) that they satisfy the above condition. Here is the algorithm :
- Make an auxiliary array of size k as Mod[k] . This array holds the count of each remainder we are getting after dividing cumulative sum till any index in arr[].
- Now start calculating cumulative sum and simultaneously take it’s mod with K, whichever remainder we get increment count by 1 for remainder as index in Mod[] auxiliary array. Sub-array by each pair of positions with same value of ( cumSum % k) constitute a continuous range whose sum is divisible by K.
- Now traverse Mod[] auxiliary array, for any Mod[i] > 1 we can choose any two pair of indices for sub-array by (Mod[i]*(Mod[i] – 1))/2 number of ways . Do the same for all remainders < k and sum up the result that will be the number all possible sub-arrays divisible by K.
C++
// C++ program to find count of subarrays with // sum divisible by k. #include <bits/stdc++.h> using namespace std; // Handles all the cases // function to find all sub-arrays divisible by k // modified to handle negative numbers as well int subCount(int arr[], int n, int k) { // create auxiliary hash array to count frequency // of remainders int mod[k]; memset(mod, 0, sizeof(mod)); // Traverse original array and compute cumulative // sum take remainder of this current cumulative // sum and increase count by 1 for this remainder // in mod[] array int cumSum = 0; for (int i = 0; i < n; i++) { cumSum += arr[i]; // as the sum can be negative, taking modulo twice mod[((cumSum % k) + k) % k]++; } int result = 0; // Initialize result // Traverse mod[] for (int i = 0; i < k; i++) // If there are more than one prefix subarrays // with a particular mod value. if (mod[i] > 1) result += (mod[i] * (mod[i] - 1)) / 2; // add the elements which are divisible by k itself // i.e., the elements whose sum = 0 result += mod[0]; return result; } // Driver program to run the case int main() { int arr[] = { 4, 5, 0, -2, -3, 1 }; int k = 5; int n = sizeof(arr) / sizeof(arr[0]); cout << subCount(arr, n, k) << endl; int arr1[] = { 4, 5, 0, -12, -23, 1 }; int k1 = 5; int n1 = sizeof(arr1) / sizeof(arr1[0]); cout << subCount(arr1, n1, k1) << endl; return 0; } // This code is corrected by Ashutosh Kumar |
Java
// Java program to find count of // subarrays with sum divisible by k. import java.util.*; class GFG { // Handles all the cases // function to find all sub-arrays divisible by k // modified to handle negative numbers as well static int subCount(int arr[], int n, int k) { // create auxiliary hash array to // count frequency of remainders int mod[] = new int[k]; Arrays.fill(mod, 0); // Traverse original array and compute cumulative // sum take remainder of this current cumulative // sum and increase count by 1 for this remainder // in mod[] array int cumSum = 0; for (int i = 0; i < n; i++) { cumSum += arr[i]; // as the sum can be negative, taking modulo twice mod[((cumSum % k) + k) % k]++; } // Initialize result int result = 0; // Traverse mod[] for (int i = 0; i < k; i++) // If there are more than one prefix subarrays // with a particular mod value. if (mod[i] > 1) result += (mod[i] * (mod[i] - 1)) / 2; // add the elements which are divisible by k itself // i.e., the elements whose sum = 0 result += mod[0]; return result; } // Driver code public static void main(String[] args) { int arr[] = { 4, 5, 0, -2, -3, 1 }; int k = 5; int n = arr.length; System.out.println(subCount(arr, n, k)); int arr1[] = { 4, 5, 0, -12, -23, 1 }; int k1 = 5; int n1 = arr1.length; System.out.println(subCount(arr1, n1, k1)); } } // This code is contributed by Anant Agarwal. |
Python3
# Python program to find # count of subarrays with # sum divisible by k. # Handles all the cases # function to find all # sub-arrays divisible by k # modified to handle # negative numbers as well def subCount(arr, n, k): # create auxiliary hash # array to count frequency # of remainders mod =[] for i in range(k + 1): mod.append(0) # Traverse original array # and compute cumulative # sum take remainder of this # current cumulative # sum and increase count by # 1 for this remainder # in mod[] array cumSum = 0 for i in range(n): cumSum = cumSum + arr[i] # as the sum can be negative, # taking modulo twice mod[((cumSum % k)+k)% k]= mod[((cumSum % k)+k)% k] + 1 result = 0 # Initialize result # Traverse mod[] for i in range(k): # If there are more than # one prefix subarrays # with a particular mod value. if (mod[i] > 1): result = result + (mod[i]*(mod[i]-1))//2 # add the elements which # are divisible by k itself # i.e., the elements whose sum = 0 result = result + mod[0] return result # driver code arr = [4, 5, 0, -2, -3, 1] k = 5n = len(arr) print(subCount(arr, n, k)) arr1 = [4, 5, 0, -12, -23, 1] k1 = 5n1 = len(arr1) print(subCount(arr1, n1, k1)) # This code is contributed # by Anant Agarwal. |
C#
// C# program to find count of // subarrays with sum divisible by k. using System; class GFG { // Handles all the cases // function to find all sub-arrays divisible by k // modified to handle negative numbers as well static int subCount(int[] arr, int n, int k) { // create auxiliary hash array to // count frequency of remainders int[] mod = new int[k]; // Traverse original array and compute cumulative // sum take remainder of this current cumulative // sum and increase count by 1 for this remainder // in mod[] array int cumSum = 0; for (int i = 0; i < n; i++) { cumSum += arr[i]; // as the sum can be negative, taking modulo twice mod[((cumSum % k) + k) % k]++; } // Initialize result int result = 0; // Traverse mod[] for (int i = 0; i < k; i++) // If there are more than one prefix subarrays // with a particular mod value. if (mod[i] > 1) result += (mod[i] * (mod[i] - 1)) / 2; // add the elements which are divisible by k itself // i.e., the elements whose sum = 0 result += mod[0]; return result; } // Driver code public static void Main() { int[] arr = { 4, 5, 0, -2, -3, 1 }; int k = 5; int n = arr.Length; Console.WriteLine(subCount(arr, n, k)); int[] arr1 = { 4, 5, 0, -12, -23, 1 }; int k1 = 5; int n1 = arr1.Length; Console.WriteLine(subCount(arr1, n1, k1)); } } // This code is contributed by vt_m. |
Output:
7 7
Time complexity : O(n + k)
Auxiliary Space : O(k)
References :
http://stackoverflow.com/questions/16605991/number-of-subarrays-divisible-by-k
This article is contributed by Shashank Mishra ( Gullu ). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Count of subarrays of size K having at least one pair with absolute difference divisible by K-1
- Generate a unique Array of length N with sum of all subarrays divisible by N
- Count of longest possible subarrays with sum not divisible by K
- Count of subarrays of an Array having all unique digits
- Count subarrays having an equal count of 0s and 1s segregated
- Count subarrays having sum of elements at even and odd positions equal
- Count of ways to split an Array into three contiguous Subarrays having increasing Sum
- Count of subarrays having sum equal to its length
- Count subarrays having sum modulo K same as the length of the subarray
- Count of subarrays having sum as a perfect cube
- Count of subarrays having sum equal to its length | Set 2
- Count sub-matrices having sum divisible 'k'
- Count number of pairs in array having sum divisible by K | SET 2
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Find all possible subarrays having product less than or equal to K
- Count subarrays having total distinct elements same as original array
- Count of subarrays having exactly K prime numbers
- Count of subarrays having exactly K perfect square numbers
- Split an Array to maximize subarrays having equal count of odd and even elements for a cost not exceeding K

