Count Subarrays With Sum Divisible By K
Last Updated :
15 Jan, 2025
Given an array arr[] and an integer k, the task is to count all subarrays whose sum is divisible by k.
Examples:
Input: arr[] = [4, 5, 0, -2, -3, 1], k = 5
Output: 7
Explanation: There are 7 subarrays whose sum is divisible by 5: [4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3] and [-2, -3].
Input: arr[] = [2, 2, 2, 2, 2, 2], k = 2
Output: 21
Explanation: All subarray sums are divisible by 2.
Input: arr[] = [-1, -3, 2], k = 5
Output: 0
Explanation: There is no subarray whose sum is divisible by k.
[Naive Approach] Iterating over all subarrays
The idea is to iterate over all possible subarrays while keeping the track of the sum of subarray modulo k. For any subarray, if the sub of subarray modulo k becomes 0, increment the count by 1. After iterating over all the subarrays, return the count as result.
C++
// C++ Code to Count Subarrays With Sum Divisible By K
// by iterating over all possible subarrays
#include <iostream>
#include <vector>
using namespace std;
int subCount(vector<int> &arr, int k) {
int n = arr.size(), res = 0;
// Iterating over starting indices of subarray
for(int i = 0; i < n; i++) {
int sum = 0;
// Iterating over ending indices of subarray
for(int j = i; j < n; j++) {
sum = (sum + arr[j]) % k;
if(sum == 0)
res += 1;
}
}
return res;
}
int main() {
vector<int> arr = {4, 5, 0, -2, -3, 1};
int k = 5;
cout << subCount(arr, k);
}
C
// C Code to Count Subarrays With Sum Divisible By K
// by iterating over all possible subarrays
#include <stdio.h>
int subCount(int arr[], int n, int k) {
int res = 0;
// Iterating over starting indices of subarray
for (int i = 0; i < n; i++) {
int sum = 0;
// Iterating over ending indices of subarray
for (int j = i; j < n; j++) {
sum = (sum + arr[j]) % k;
if (sum == 0)
res += 1;
}
}
return res;
}
int main() {
int arr[] = {4, 5, 0, -2, -3, 1};
int k = 5;
int n = sizeof(arr) / sizeof(arr[0]);
printf("%d", subCount(arr, n, k));
return 0;
}
Java
// Java Code to Count Subarrays With Sum Divisible By K
// by iterating over all possible subarrays
import java.util.*;
class GfG {
static int subCount(int[] arr, int k) {
int n = arr.length, res = 0;
// Iterating over starting indices of subarray
for (int i = 0; i < n; i++) {
int sum = 0;
// Iterating over ending indices of subarray
for (int j = i; j < n; j++) {
sum = (sum + arr[j]) % k;
if (sum == 0)
res += 1;
}
}
return res;
}
public static void main(String[] args) {
int[] arr = {4, 5, 0, -2, -3, 1};
int k = 5;
System.out.println(subCount(arr, k));
}
}
Python
# Python Code to Count Subarrays With Sum Divisible By K
# by iterating over all possible subarrays
def subCount(arr, k):
n = len(arr)
res = 0
# Iterating over starting indices of subarray
for i in range(n):
sum = 0
# Iterating over ending indices of subarray
for j in range(i, n):
sum = (sum + arr[j]) % k
if sum == 0:
res += 1
return res
if __name__ == "__main__":
arr = [4, 5, 0, -2, -3, 1]
k = 5
print(subCount(arr, k))
C#
// C# Code to Count Subarrays With Sum Divisible By K
// by iterating over all possible subarrays
using System;
using System.Collections.Generic;
class GfG {
static int subCount(int[] arr, int k) {
int n = arr.Length, res = 0;
// Iterating over starting indices of subarray
for (int i = 0; i < n; i++) {
int sum = 0;
// Iterating over ending indices of subarray
for (int j = i; j < n; j++) {
sum = (sum + arr[j]) % k;
if (sum == 0)
res += 1;
}
}
return res;
}
static void Main() {
int[] arr = { 4, 5, 0, -2, -3, 1 };
int k = 5;
Console.WriteLine(subCount(arr, k));
}
}
JavaScript
// JavaScript Code to Count Subarrays With Sum Divisible By K
// by iterating over all possible subarrays
function subCount(arr, k) {
let n = arr.length, res = 0;
// Iterating over starting indices of subarray
for (let i = 0; i < n; i++) {
let sum = 0;
// Iterating over ending indices of subarray
for (let j = i; j < n; j++) {
sum = (sum + arr[j]) % k;
if (sum === 0)
res += 1;
}
}
return res;
}
// Driver Code
let arr = [4, 5, 0, -2, -3, 1];
let k = 5;
console.log(subCount(arr, k));
Time Complexity: O(n^2), as we are iterating over all possible starting and ending points of subarrays.
Auxiliary Space: O(1)
[Expected Approach] Using Prefix Sum modulo k
The idea is to use Prefix Sum Technique along with Hashing. On observing carefully, we can say that if a subarray arr[i…j] has sum divisible by k, then (prefix sum[i] % k) will be equal to the (prefix sum[j] % k). So, we can iterate over arr[] while maintaining a hash map or dictionary to count the number of (prefix sum mod k). For each index i, the number of subarrays ending at i and having sum divisible by k will be equal to the count of occurrences of (prefix sum[i] mod k) before i.
Note: Negative value of (prefix sum mod k) needs to be handled separately in languages like C++, Java, C# and JavaScript, whereas in Python (prefix sum mod k) is always a non-negative value as it takes the sign of the divisor, that is k.
C++
// C++ Code to Count Subarrays With Sum Divisible By K
// using Prefix Sum and Hash map
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
int subCount(vector<int> &arr, int k) {
int n = arr.size(), res = 0;
unordered_map<int, int> prefCnt;
int sum = 0;
// Iterate over all ending points
for(int i = 0; i < n; i++) {
// prefix sum mod k (handling negative prefix sum)
sum = ((sum + arr[i]) % k + k) % k;
// If sum == 0, then increment the result by 1
// to count subarray arr[0...i]
if(sum == 0)
res += 1;
// Add count of all starting points for index i
res += prefCnt[sum];
prefCnt[sum] += 1;
}
return res;
}
int main() {
vector<int> arr = {4, 5, 0, -2, -3, 1};
int k = 5;
cout << subCount(arr, k);
}
Java
// Java Code to Count Subarrays With Sum Divisible By K
// using Prefix Sum and Hash map
import java.util.*;
class GfG {
static int subCount(int[] arr, int k) {
int n = arr.length, res = 0;
Map<Integer, Integer> prefCnt = new HashMap<>();
int sum = 0;
// Iterate over all ending points
for (int i = 0; i < n; i++) {
// prefix sum mod k (handling negative prefix sum)
sum = ((sum + arr[i]) % k + k) % k;
// If sum == 0, then increment the result by 1
// to count subarray arr[0...i]
if (sum == 0)
res += 1;
// Add count of all starting points for index i
res += prefCnt.getOrDefault(sum, 0);
prefCnt.put(sum, prefCnt.getOrDefault(sum, 0) + 1);
}
return res;
}
public static void main(String[] args) {
int[] arr = {4, 5, 0, -2, -3, 1};
int k = 5;
System.out.println(subCount(arr, k));
}
}
Python
# Python Code to Count Subarrays With Sum Divisible By K
# using Prefix Sum and Dictionary
from collections import defaultdict
def subCount(arr, k):
n = len(arr)
res = 0
prefCnt = defaultdict(int)
sum = 0
# Iterate over all ending points
for i in range(n):
sum = (sum + arr[i]) % k
# If sum == 0, then increment the result by 1
# to count subarray arr[0...i]
if sum == 0:
res += 1
# Add count of all starting points for index i
res += prefCnt[sum]
prefCnt[sum] += 1
return res
if __name__ == "__main__":
arr = [4, 5, 0, -2, -3, 1]
k = 5
print(subCount(arr, k))
C#
// C# Code to Count Subarrays With Sum Divisible By K
// using Prefix Sum and Hash map
using System;
using System.Collections.Generic;
class GfG {
static int SubCount(int[] arr, int k) {
int n = arr.Length, res = 0;
Dictionary<int, int> prefCnt = new Dictionary<int, int>();
int sum = 0;
// Iterate over all ending points
for (int i = 0; i < n; i++) {
// prefix sum mod k (handling negative prefix sum)
sum = ((sum + arr[i]) % k + k) % k;
// If sum == 0, then increment the result by 1
// to count subarray arr[0...i]
if (sum == 0)
res += 1;
// Add count of all starting points for index i
if (prefCnt.ContainsKey(sum))
res += prefCnt[sum];
if (prefCnt.ContainsKey(sum))
prefCnt[sum] += 1;
else
prefCnt[sum] = 1;
}
return res;
}
static void Main() {
int[] arr = { 4, 5, 0, -2, -3, 1 };
int k = 5;
Console.WriteLine(SubCount(arr, k));
}
}
JavaScript
// JavaScript Code to Count Subarrays With Sum Divisible By K
// using Prefix Sum and Hash map
function subCount(arr, k) {
let n = arr.length, res = 0;
let prefCnt = new Map();
let sum = 0;
// Iterate over all ending points
for (let i = 0; i < n; i++) {
// prefix sum mod k (handling negative prefix sum)
sum = ((sum + arr[i]) % k + k) % k;
// If sum == 0, then increment the result by 1
// to count subarray arr[0...i]
if (sum === 0)
res += 1;
// Add count of all starting points for index i
res += (prefCnt.get(sum) || 0);
prefCnt.set(sum, (prefCnt.get(sum) || 0) + 1);
}
return res;
}
// Driver Code
let arr = [4, 5, 0, -2, -3, 1];
let k = 5;
console.log(subCount(arr, k));
Time Complexity: O(n), as we are iterating over the array only once.
Auxiliary Space: O(min(n, k)), as at most k keys can be present in the hash map or dictionary.
Similar Reads
Longest Subarray With Sum Divisible By K
Given an arr[] containing n integers and a positive integer k, he problem is to find the longest subarray's length with the sum of the elements divisible by k. Examples: Input: arr[] = [2, 7, 6, 1, 4, 5], k = 3Output: 4Explanation: The subarray [7, 6, 1, 4] has sum = 18, which is divisible by 3. Inp
10 min read
Subarray with no pair sum divisible by K
Given an array of N non-negative integers, task is to find the maximum size of a subarray such that the pairwise sum of the elements of this subarray is not divisible by a given integer, K. Also, print this subarray as well. If there are two or more subarrays that follow the above stated condition,
13 min read
Subset with sum divisible by m
Given a set of non-negative distinct integers, and a value m, determine if there is a subset of the given set with sum divisible by m. Input Constraints Size of set i.e., n <= 1000000, m <= 1000Examples: Input : arr[] = {3, 1, 7, 5}; m = 6; Output : YES Input : arr[] = {1, 6}; m = 5; Output :
15+ min read
Count of longest possible subarrays with sum not divisible by K
Given an array of integers arr[] and a positive integer K, the task is to find the count of the longest possible subarrays with sum of its elements not divisible by K. Examples: Input: arr[] = {2, 3, 4, 6}, K = 3 Output: 1 Explanation: There is only one longest possible subarray of size 3 i.e. {3, 4
9 min read
Count of subarrays with sum at least K
Given an array arr[] of size N and an integer K > 0. The task is to find the number of subarrays with sum at least K.Examples: Input: arr[] = {6, 1, 2, 7}, K = 10 Output: 2 {6, 1, 2, 7} and {1, 2, 7} are the only valid subarrays.Input: arr[] = {3, 3, 3}, K = 5 Output: 3 Approach: For a fixed left
6 min read
Subset with no pair sum divisible by K
Given an array of integer numbers, we need to find maximum size of a subset such that sum of each pair of this subset is not divisible by K. Examples : Input : arr[] = [3, 7, 2, 9, 1] K = 3 Output : 3 Maximum size subset whose each pair sum is not divisible by K is [3, 7, 1] because, 3+7 = 10, 3+1 =
7 min read
Count Subarrays having Sum K
Given an unsorted array of integers, the task is to find the number of subarrays having a sum exactly equal to a given number k. Examples: Input : arr[] = [10, 2, -2, -20, 10], k = -10Output : 3Explanation: Subarrays: arr[0...3], arr[1...4], arr[3...4] have sum equal to -10. Input : arr[] = [9, 4, 2
8 min read
Count pairs in array whose sum is divisible by K
Given an array A[] and positive integer K, the task is to count the total number of pairs in the array whose sum is divisible by K. Note: This question is a generalized version of this Examples: Input : A[] = {2, 2, 1, 7, 5, 3}, K = 4 Output : 5 Explanation : There are five pairs possible whose sum
10 min read
Count pairs in array whose sum is divisible by 4
Given a array if 'n' positive integers. Count number of pairs of integers in the array that have the sum divisible by 4. Examples : Input: {2, 2, 1, 7, 5}Output: 3Explanation:Only three pairs are possible whose sumis divisible by '4' i.e., (2, 2), (1, 7) and (7, 5)Input: {2, 2, 3, 5, 6}Output: 4Reco
10 min read
Count of subarrays with digit sum equals to X
Given an array arr[] of length N and integer X, the task is to count no of subarrays having digit sum equal to X. Examples: Input: arr[] = {10, 5, 13, 20, 9}, X = 6Output: 2Explanation: There are two subarrays which is having digit sum equal to 6. {10, 5} => (1 + 0) + 5 = 6 and {13 , 20} => (1
9 min read