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.**Examples:**

Input :A[] = {2, 2, 1, 7, 5, 3}, K = 4Output :5

There are five pairs possible whose sum

Is divisible by ‘4’ i.e., (2, 2),

(1, 7), (7, 5), (1, 3) and (5, 3)Input :A[] = {5, 9, 36, 74, 52, 31, 42}, K = 3Output :7

**Approach:** In the previous post, an approach using hashing is discussed. In this article, another approach using hashing is discussed.

Analyzing the statement we can say we need to make pairs of (a, b) such that:

(a + b) % K = 0 => a%K + b%K = 0 => a%K + b%K = K%K => b%K = K%K - a%K => b%K = (K - a%K) % K. {Range of a%K => [0,K-1]}

The idea is * a *can be paired with (K — a%K) % K. Now we have to find the same for each

*present in the given array.*

**a**The algorithm would create a hash-map:**Keys: **possible remainders for value%K i.e 0 to K-1 **Values: **count of values with value%K = key

The stepwise algorithm is:

- Find x = arr[i]%k.
- This array element can be paired with array elements having mod value k-x. This frequency count of array elements is stored in hash. So add that count to answer.
- Increment count for x in hash.
- In case the value of x is zero, then it can be paired only with elements having 0 mod value.

Below is the implementation of the above approach:

## C++

`// C++ Program to count pairs` `// whose sum divisible by 'K'` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Program to count pairs whose sum divisible` `// by 'K'` `int` `countKdivPairs(` `int` `A[], ` `int` `n, ` `int` `K)` `{` ` ` `// Create a frequency array to count` ` ` `// occurrences of all remainders when` ` ` `// divided by K` ` ` `int` `freq[K] = { 0 };` ` ` `// To store count of pairs.` ` ` `int` `ans = 0;` ` ` `// Traverse the array, compute the remainder` ` ` `// and add k-remainder value hash count to ans` ` ` `for` `(` `int` `i = 0; i < n; i++) {` ` ` `int` `rem = A[i] % K;` ` ` ` ` `// Count number of ( A[i], (K - rem)%K ) pairs` ` ` `ans += freq[(K - rem) % K];` ` ` `// Increment count of remainder in hash map` ` ` `freq[rem]++;` ` ` `}` ` ` `return` `ans;` `}` `// Driver code` `int` `main()` `{` ` ` `int` `A[] = { 2, 2, 1, 7, 5, 3 };` ` ` `int` `n = ` `sizeof` `(A) / ` `sizeof` `(A[0]);` ` ` `int` `K = 4;` ` ` `cout << countKdivPairs(A, n, K);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// JAVA Program to count pairs whose sum divisible` `// by 'K'` `class` `GFG ` `{` ` ` `static` `int` `countKdivPairs(` `int` `A[], ` `int` `n, ` `int` `K)` ` ` `{` ` ` `// Create a frequency array to count` ` ` `// occurrences of all remainders when` ` ` `// divided by K` ` ` `int` `[]freq = ` `new` `int` `[K];` ` ` `// To store count of pairs.` ` ` `int` `ans = ` `0` `;` ` ` `// Traverse the array, compute the remainder` ` ` `// and add k-remainder value hash count to ans` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++)` ` ` `{` ` ` `int` `rem = A[i] % K;` ` ` `// Count number of ( A[i], (K - rem)%K ) pairs` ` ` `ans += freq[(K - rem) % K];` ` ` `// Increment count of remainder in hash map` ` ` `freq[rem]++;` ` ` `}` ` ` `return` `ans;` ` ` `}` `// Driver code` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{` ` ` `int` `A[] = { ` `2` `, ` `2` `, ` `1` `, ` `7` `, ` `5` `, ` `3` `};` ` ` `int` `n = A.length;` ` ` `int` `K = ` `4` `;` ` ` `System.out.println(countKdivPairs(A, n, K));` ` ` `}` `}` `// This code is contributed by Princi Singh, Yadvendra Naveen` |

*chevron_right*

*filter_none*

## Python3

`# Python Program to count pairs whose sum divisible` `# by 'K'` `def` `countKdivPairs(A, n, K):` ` ` ` ` `# Create a frequency array to count` ` ` `# occurrences of all remainders when` ` ` `# divided by K` ` ` `freq ` `=` `[` `0` `for` `i ` `in` `range` `(K)]` ` ` `# To store count of pairs.` ` ` `ans ` `=` `0` ` ` `# Traverse the array, compute the remainder` ` ` `# and add k-remainder value hash count to ans` ` ` `for` `i ` `in` `range` `(n):` ` ` `rem ` `=` `A[i] ` `%` `K` ` ` ` ` `# Count number of ( A[i], (K - rem)%K ) pairs` ` ` `ans ` `+` `=` `freq[(K ` `-` `rem) ` `%` `K]` ` ` ` ` `# Increment count of remainder in hash map` ` ` `freq[rem] ` `+` `=` `1` ` ` `return` `ans` `# Driver code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` `A ` `=` `[` `2` `, ` `2` `, ` `1` `, ` `7` `, ` `5` `, ` `3` `]` ` ` `n ` `=` `len` `(A)` ` ` `K ` `=` `4` ` ` `print` `(countKdivPairs(A, n, K))` `# This code is contributed by` `# Surendra_Gangwar, Yadvendra Naveen` |

*chevron_right*

*filter_none*

## C#

`// C# Program to count pairs` `// whose sum divisible by 'K'` `using` `System;` `class` `GFG ` `{` `// Program to count pairs whose sum divisible` `// by 'K'` `static` `int` `countKdivPairs(` `int` `[]A, ` `int` `n, ` `int` `K)` `{` ` ` `// Create a frequency array to count` ` ` `// occurrences of all remainders when` ` ` `// divided by K` ` ` `int` `[]freq = ` `new` `int` `[K];` ` ` `// To store count of pairs.` ` ` `int` `ans = 0;` ` ` `// Traverse the array, compute the remainder` ` ` `// and add k-remainder value hash count to ans` ` ` `for` `(` `int` `i = 0; i < n; i++)` ` ` `{` ` ` `int` `rem = A[i] % K;` ` ` `// Count number of ( A[i], (K - rem)%K ) pairs` ` ` `ans += freq[(K - rem) % K];` ` ` `// Increment count of remainder in hash map` ` ` `freq[rem]++;` ` ` `}` ` ` `return` `ans;` `}` `// Driver code` `public` `static` `void` `Main(String[] args) ` `{` ` ` `int` `[]A = { 2, 2, 1, 7, 5, 3 };` ` ` `int` `n = A.Length;` ` ` `int` `K = 4;` ` ` `Console.WriteLine(countKdivPairs(A, n, K));` `}` `}` `// This code contributed by Rajput-Ji, Yadvendra Naveen` |

*chevron_right*

*filter_none*

**Output:**

5

**Time Complexity:** O(N) **Auxiliary Space:** O(K)

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 the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i]
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count pairs in array whose sum is divisible by 4
- Count pairs in array whose sum is divisible by K
- Length of longest subsequence having absolute difference of all pairs divisible by K
- Split array into two subsequences having minimum count of pairs with sum equal to X
- Count pairs from an array having product of their sum and difference equal to 1
- Count pairs from an array having product of their sum and difference equal to 0
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Count all sub-arrays having sum divisible by k
- 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
- Maximize count of pairs (i, j) from two arrays having element from first array not exceeding that from second array
- Number of pairs in an array having sum equal to product
- Count pairs of numbers from 1 to N with Product divisible by their Sum
- Count pairs from 1 to N such that their Sum is divisible by their XOR
- Count of pairs from 1 to a and 1 to b whose sum is divisible by N
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Ways to form an array having integers in given range such that total sum is divisible by 2
- Maximize the numbers of splits in an Array having sum divisible by 3

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.