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
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
Naive Approach: The simplest approach is to iterate through every pair of the array but using two nested for loops and count those pairs whose sum is divisible by ‘K’. The time complexity of this approach is O(N2).
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 count = 0;
for ( int i = 0; i < n; i++) {
for ( int j = i + 1; j < n; j++) {
if ((A[i] + A[j]) % K == 0)
count++;
}
}
return count;
}
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
import java.io.*;
class GFG {
static int countKdivPairs( int [] A, int n, int K)
{
int count = 0 ;
for ( int i = 0 ; i < n; i++) {
for ( int j = i + 1 ; j < n; j++) {
if ((A[i] + A[j]) % K == 0 )
count++;
}
}
return count;
}
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));
}
}
|
C#
using System;
class GFG {
static int countKdivPairs( int [] A, int n, int K)
{
int count = 0;
for ( int i = 0; i < n; i++) {
for ( int j = i + 1; j < n; j++) {
if ((A[i] + A[j]) % K == 0)
count++;
}
}
return count;
}
public static void Main()
{
int [] A = { 2, 2, 1, 7, 5, 3 };
int n = A.Length;
int K = 4;
Console.Write(countKdivPairs(A, n, K));
}
}
|
Javascript
function countKdivPairs(A, n, K)
{
let count = 0
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
if ((A[i] + A[j]) % K == 0)
count++
}
}
return count
}
let A = [ 2, 2, 1, 7, 5, 3 ]
let n = A.length
let K = 4
console.log(countKdivPairs(A, n, K))
|
Python3
def countKdivPairs(A, n, K):
count = 0
for i in range ( 0 , n):
for j in range (i + 1 , n):
if ((A[i] + A[j]) % K = = 0 ):
count + = 1
return count
A = [ 2 , 2 , 1 , 7 , 5 , 3 ]
n = len (A)
K = 4
print (countKdivPairs(A, n, K))
|
Time complexity: O(N2), for using two nested loops.
Auxiliary Space: O(1), as constant space is used.
Efficient Approach: An efficient approach is to use Hashing technique. We will separate elements into buckets depending on their (value mod K). When a number is divided by K then the remainder may be 0, 1, 2, up to (k-1). So take an array to say freq[] of size K (initialized with Zero) and increase the value of freq[A[i]%K] so that we can calculate the number of values giving remainder j on division with K.
C++
#include <bits/stdc++.h>
using namespace std;
int countKdivPairs( int A[], int n, int K)
{
int freq[K] = { 0 };
for ( int i = 0; i < n; i++)
++freq[A[i] % K];
int sum = freq[0] * (freq[0] - 1) / 2;
for ( int i = 1; i <= K / 2 && i != (K - i); i++)
sum += freq[i] * freq[K - i];
if (K % 2 == 0)
sum += (freq[K / 2] * (freq[K / 2] - 1) / 2);
return sum;
}
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
import java.util.*;
class Count {
public static int countKdivPairs( int A[], int n, int K)
{
int freq[] = new int [K];
for ( int i = 0 ; i < n; i++)
++freq[A[i] % K];
int sum = freq[ 0 ] * (freq[ 0 ] - 1 ) / 2 ;
for ( int i = 1 ; i <= K / 2 && i != (K - i); i++)
sum += freq[i] * freq[K - i];
if (K % 2 == 0 )
sum += (freq[K / 2 ] * (freq[K / 2 ] - 1 ) / 2 );
return sum;
}
public static void main(String[] args)
{
int A[] = { 2 , 2 , 1 , 7 , 5 , 3 };
int n = 6 ;
int K = 4 ;
System.out.print(countKdivPairs(A, n, K));
}
}
|
Python3
def countKdivPairs(A, n, K):
freq = [ 0 ] * K
for i in range (n):
freq[A[i] % K] + = 1
sum = freq[ 0 ] * (freq[ 0 ] - 1 ) / 2 ;
i = 1
while (i < = K / / 2 and i ! = (K - i) ):
sum + = freq[i] * freq[K - i]
i + = 1
if ( K % 2 = = 0 ):
sum + = (freq[K / / 2 ] * (freq[K / / 2 ] - 1 ) / 2 );
return int ( sum )
A = [ 2 , 2 , 1 , 7 , 5 , 3 ]
n = len (A)
K = 4
print (countKdivPairs(A, n, K))
|
C#
using System;
class Count
{
public static int countKdivPairs( int []A, int n, int K)
{
int []freq = new int [K];
for ( int i = 0; i < n; i++)
++freq[A[i] % K];
int sum = freq[0] * (freq[0] - 1) / 2;
for ( int i = 1; i <= K / 2 && i != (K - i); i++)
sum += freq[i] * freq[K - i];
if (K % 2 == 0)
sum += (freq[K / 2] * (freq[K / 2] - 1) / 2);
return sum;
}
static public void Main ()
{
int []A = { 2, 2, 1, 7, 5, 3 };
int n = 6;
int K = 4;
Console.WriteLine(countKdivPairs(A, n, K));
}
}
|
PHP
<?php
function countKdivPairs( $A , $n , $K )
{
$freq = array_fill (0, $K , 0);
for ( $i = 0; $i < $n ; $i ++)
++ $freq [ $A [ $i ] % $K ];
$sum = (int)( $freq [0] * ( $freq [0] - 1) / 2);
for ( $i = 1; $i <= $K / 2 &&
$i != ( $K - $i ); $i ++)
$sum += $freq [ $i ] * $freq [ $K - $i ];
if ( $K % 2 == 0)
$sum += (int)( $freq [(int)( $K / 2)] *
( $freq [(int)( $K / 2)] - 1) / 2);
return $sum ;
}
$A = array ( 2, 2, 1, 7, 5, 3 );
$n = count ( $A );
$K = 4;
echo countKdivPairs( $A , $n , $K );
?>
|
Javascript
<script>
function countKdivPairs(A, n, K)
{
let freq = new Array(K);
freq.fill(0);
for (let i = 0; i < n; i++)
++freq[A[i] % K];
let sum = freq[0] * parseInt((freq[0] - 1) / 2, 10);
for (let i = 1; i <= K / 2 && i != (K - i); i++)
sum += freq[i] * freq[K - i];
if (K % 2 == 0)
sum += parseInt(freq[parseInt(K / 2, 10)] * (freq[parseInt(K / 2, 10)] - 1) / 2, 10);
return sum;
}
let A = [ 2, 2, 1, 7, 5, 3 ];
let n = 6;
let K = 4;
document.write(countKdivPairs(A, n, K));
</script>
|
Time complexity: O(N)
Auxiliary space: O(K), since K extra space has been taken.
https://www.youtube.com/watch?v=5UJvXcSUyT0