📜  计算具有可被K |整除的数组中的对数设置2

📅  最后修改于: 2021-04-22 09:49:43             🧑  作者: Mango

给定一个数组A []和正整数K,任务是计算该数组的和可被K整除的对的总数。
例子:

方法:在上一篇文章中,讨论了使用哈希的方法。在本文中,讨论了使用哈希的另一种方法。

分析该语句,我们可以说我们需要对(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]}

这个想法可以用(K -一%K)配对%K.现在我们必须找到给定数组中的每一个存在于相同。

该算法将创建一个哈希映射:
键:值%K的可能余数,即0到K-1
值:具有%K =键的值的计数

逐步算法为:

  1. 找出x = arr [i]%k。
  2. 该数组元素可以与具有mod值kx的数组元素配对。数组元素的此频率计数存储在哈希中。因此,添加该计数即可回答。
  3. x在哈希中的增量计数。
  4. 如果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


输出:
5


时间复杂度: O(N)
辅助空间: O(K)