Count number of pairs in array having sum divisible by K | SET 2
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 = 4
Output : 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 = 3
Output : 7
Approach: In the previous post, an approach using hashing is discussed. In this article, another approach using hashing is discussed.
The idea is to traverse the array, find (arr[i] % k) and keep track of these values in the hash.
The stepwise algorithm is:
- Find x = arr[i]%k.
- This array element can be paired with array elements having mod value k-x. This count of array elements is stored in hash. So add that count to answer.
- Increment count for x in hash.
- In case 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; if (rem != 0) ans += freq[K - rem]; else ans += freq[0]; // 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; } |
Java
// Java Program to count pairs // whose sum divisible by 'K' 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; if (rem != 0) ans += freq[K - rem]; else ans += freq[0]; // 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 |
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;
if (rem != 0)
ans += freq[K - rem];
else
ans += freq[0];
// 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
[tabbyending]
5
Time Complexity: O(N)
Auxiliary Space: O(1)
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 pairs in array whose sum is divisible by K
- Count divisible pairs in an array
- Count pairs in array whose sum is divisible by 4
- Count the number of elements in an array which are divisible by k
- Count number of distinct pairs whose sum exists in the given array
- Count pairs (i,j) such that (i+j) is divisible by A and B both
- Count of pairs from 1 to a and 1 to b whose sum is divisible by N
- Count pairs of numbers from 1 to N with Product divisible by their Sum
- Check if an array can be divided into pairs whose sum is divisible by k
- Maximize the number of sum pairs which are divisible by K
- Number of pairs from the first N natural numbers whose sum is divisible by K
- Count of pairs (x, y) in an array such that x < y
- Count pairs in an array such that LCM(arr[i], arr[j]) > min(arr[i],arr[j])
- Count pairs in an array that hold i+j= arr[i]+arr[j]
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.
Improved By : princi singh



