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.
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 a present in the given array.
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++
#include <bits/stdc++.h>
using namespace std;
int countKdivPairs( int A[], int n, int K)
{
int freq[K] = { 0 };
int ans = 0;
for ( int i = 0; i < n; i++) {
int rem = A[i] % K;
ans += freq[(K - rem) % K];
freq[rem]++;
}
return ans;
}
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
class GFG
{
static int countKdivPairs( int A[], int n, int K)
{
int []freq = new int [K];
int ans = 0 ;
for ( int i = 0 ; i < n; i++)
{
int rem = A[i] % K;
ans += freq[(K - rem) % K];
freq[rem]++;
}
return ans;
}
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));
}
}
|
Python3
def countKdivPairs(A, n, K):
freq = [ 0 for i in range (K)]
ans = 0
for i in range (n):
rem = A[i] % K
ans + = freq[(K - rem) % K]
freq[rem] + = 1
return ans
if __name__ = = '__main__' :
A = [ 2 , 2 , 1 , 7 , 5 , 3 ]
n = len (A)
K = 4
print (countKdivPairs(A, n, K))
|
C#
using System;
class GFG
{
static int countKdivPairs( int []A, int n, int K)
{
int []freq = new int [K];
int ans = 0;
for ( int i = 0; i < n; i++)
{
int rem = A[i] % K;
ans += freq[(K - rem) % K];
freq[rem]++;
}
return ans;
}
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));
}
}
|
Javascript
<script>
function countKdivPairs( A, n, K)
{
var freq = Array(K).fill(0);
var ans = 0;
for ( var i = 0; i < n; i++) {
var rem = A[i] % K;
ans += freq[(K - rem) % K];
freq[rem]++;
}
return ans;
}
var A = [ 2, 2, 1, 7, 5, 3 ];
var n = A.length;
var K = 4;
document.write( countKdivPairs(A, n, K));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(K)
Last Updated :
23 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...