给定一个数组 A[] 和正整数 K,任务是计算数组中总和可被 K 整除的对的总数。
例子:
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
方法:在上一篇文章中,讨论了使用散列的方法。在本文中,讨论了另一种使用散列的方法。
分析语句,我们可以说我们需要对 (a, b) 进行如下组合:
(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]}
这个想法是a可以与 (K — a%K) % K 配对。现在我们必须为给定数组中的每个a找到相同的存在。
该算法将创建一个哈希映射:
键:值%K 的可能余数,即 0 到 K-1
值: value%K = key 的值计数
逐步算法是:
- 求 x = arr[i]%k。
- 该数组元素可以与具有 mod 值 kx 的数组元素配对。数组元素的频率计数存储在哈希中。所以加上那个计数来回答。
- 哈希中 x 的增量计数。
- 如果 x 的值为零,则它只能与具有 0 mod 值的元素配对。
下面是上述方法的实现:
C++
// C++ Program to count pairs
// whose sum divisible by 'K'
#include
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;
}
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
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
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
Javascript
输出:
5
时间复杂度: O(N)
辅助空间: O(K)