📌  相关文章
📜  对数组中的对进行计数,其和可被4整除

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

给定一个数组,如果’n’个正整数。计算数组中可被4整除的整数对的数量。
例子 :

Input: {2, 2, 1, 7, 5}
Output: 3

Explanation
Only three pairs are possible whose sum
is divisible by '4' i.e., (2, 2), 
(1, 7) and (7, 5)

Input: {2, 2, 3, 5, 6}
Output: 4

天真的方法是使用两个嵌套的for循环遍历每对数组bu,并对总和可被“ 4”整除的那些对进行计数。该方法的时间复杂度为O(n 2 )。

高效的方法是使用哈希技术。只有三种情况会出现,其总和可被“ 4”整除,即,

  1. 如果两者都可被4整除。
  2. 如果其中一个等于1模4,而另一个等于3模4 。例如(1、3),(5、7),(5、11)。
  3. 如果它们都等于2模4,即(2,2),(2,6),(6,10)
    将所有模存储在freq []数组中,以使freq [i] =等于i模4的数组元素

因此答案=> \implies \displaystyle {{freq[0]} \choose 2} + {freq[2] \choose 2} + freq[1] \cdot freq[3]\implies \displaystyle \frac{freq_0 (freq_0-1)}{2} +\frac{freq_2 (freq_2-1)}{2} +         freq_1 \cdot freq_3

C++
// C++ Program to count pairs 
// whose sum divisible by '4'
#include 
using namespace std;
  
// Program to count pairs whose sum divisible
// by '4'
int count4Divisibiles(int arr[], int n)
{
    // Create a frequency array to count 
    // occurrences of all remainders when 
    // divided by 4
    int freq[4] = {0, 0, 0, 0};
  
    // Count occurrences of all remainders
    for (int i = 0; i < n; i++)
        ++freq[arr[i] % 4];
  
    // If both pairs are divisible by '4'
    int ans = freq[0] * (freq[0] - 1) / 2;
  
    // If both pairs are 2 modulo 4
    ans += freq[2] * (freq[2] - 1) / 2;
  
    // If one of them is equal
    // to 1 modulo 4 and the
    // other is equal to 3 
    // modulo 4
    ans += freq[1] * freq[3];
  
    return ans;
}
  
// Driver code
int main()
{
  
    int arr[] = { 2, 2, 1, 7, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << count4Divisibiles(arr, n);
  
    return 0;
}


Java
// Java program to count pairs 
// whose sum divisible by '4'
import java.util.*;
  
class Count{
    public static int count4Divisibiles(int arr[] , 
                                             int n )
    {
        // Create a frequency array to count 
        // occurrences of all remainders when 
        // divided by 4
        int freq[] = {0, 0, 0, 0};
        int i = 0;
        int ans;
          
        // Count occurrences of all remainders
        for (i = 0; i < n; i++)
                ++freq[arr[i] % 4];
          
        //If both pairs are divisible by '4'
        ans = freq[0] * (freq[0] - 1) / 2;
      
        // If both pairs are 2 modulo 4
        ans += freq[2] * (freq[2] - 1) / 2;
      
        // If one of them is equal
        // to 1 modulo 4 and the
        // other is equal to 3 
        // modulo 4
        ans += freq[1] * freq[3];
      
        return (ans);
    }
    public static void main(String[] args)
    {
        int arr[] = {2, 2, 1, 7, 5};
        int n = 5;
        System.out.print(count4Divisibiles(arr, n));
    }
}
  
// This code is contributed by rishabh_jain


Python3
# Python3 code to count pairs whose 
# sum is divisible by '4'
  
# Function to count pairs whose 
# sum is divisible by '4'
def count4Divisibiles( arr , n ):
      
    # Create a frequency array to count 
    # occurrences of all remainders when 
    # divided by 4
    freq = [0, 0, 0, 0]
      
    # Count occurrences of all remainders
    for i in range(n):
        freq[arr[i] % 4]+=1
          
    #If both pairs are divisible by '4'
    ans = freq[0] * (freq[0] - 1) / 2
      
    # If both pairs are 2 modulo 4
    ans += freq[2] * (freq[2] - 1) / 2
      
    # If one of them is equal
    # to 1 modulo 4 and the
    # other is equal to 3 
    # modulo 4
    ans += freq[1] * freq[3]
      
    return int(ans)
  
# Driver code
arr = [2, 2, 1, 7, 5]
n = len(arr)
print(count4Divisibiles(arr, n))
  
# This code is contributed by "Sharad_Bhardwaj".


C#
// C# program to count pairs 
// whose sum divisible by '4'
using System;
  
class Count{
    public static int count4Divisibiles(int []arr , 
                                            int n )
    {
        // Create a frequency array to count 
        // occurrences of all remainders when 
        // divided by 4
        int []freq = {0, 0, 0, 0};
        int i = 0;
        int ans;
          
        // Count occurrences of all remainders
        for (i = 0; i < n; i++)
            ++freq[arr[i] % 4];
          
        //If both pairs are divisible by '4'
        ans = freq[0] * (freq[0] - 1) / 2;
      
        // If both pairs are 2 modulo 4
        ans += freq[2] * (freq[2] - 1) / 2;
      
        // If one of them is equal
        // to 1 modulo 4 and the
        // other is equal to 3 
        // modulo 4
        ans += freq[1] * freq[3];
      
        return (ans);
    }
      
    // Driver code
    public static void Main()
    {
        int []arr = {2, 2, 1, 7, 5};
        int n = 5;
        Console.WriteLine(count4Divisibiles(arr, n));
    }
}
  
// This code is contributed by vt_m


PHP


输出 :

3

时间复杂度: O(n)
辅助空间: O(1)