📜  对具有相等的按位与和按位或值的对进行计数

📅  最后修改于: 2021-04-22 06:48:12             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是计算无序对的数量,以使每对的按位与和按位或相等。

例子:

天真的方法:解决问题的最简单方法是遍历数组并生成给定数组的所有可能对。对于每对,检查该对的按位与是否等于该对的按位或。如果发现为真,则增加计数器。最后,打印计数器的值。

高效方法:为了优化上述方法,该思想基于以下观察结果:

请按照以下步骤解决问题:

  • 初始化一个变量,例如cntPairs,以存储按位AND(&)值和按位OR(|)值相等的对的计数。
  • 创建一个映射图,例如mp,以存储给定数组中所有不同元素的频率。
  • 遍历给定的数组,并将给定数组的所有不同元素的频率存储在mp中
  • 遍历地图并检查频率(例如,频率是否大于1),然后更新cntPairs + =(频率*(频率– 1))/ 2。
  • 最后,打印cntPairs的值

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to count pairs in an array
// whose bitwise AND equal to bitwise OR
int countPairs(int arr[], int N)
{
     
    // Store count of pairs whose
    // bitwise AND equal to bitwise OR
    int cntPairs = 0;
     
    // Stores frequency of
    // distinct elements of array
    map mp;
     
    // Traverse the array
    for (int i = 0; i < N; i++) {
         
        // Increment the frequency
        // of arr[i]
        mp[arr[i]]++;
    }
     
    // Traverse map
    for (auto freq: mp) {
        cntPairs += (freq.second *
                   (freq.second - 1)) / 2;
    }
     
    return cntPairs;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 1, 2, 2 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout<


Java
// Java program to implement
// the above approach
import java.io.*;
import java.util.*;
 
class GFG{
 
// Function to count pairs in an array
// whose bitwise AND equal to bitwise OR
static int countPairs(int[] arr, int N)
{
     
    // Store count of pairs whose
    // bitwise AND equal to bitwise OR
    int cntPairs = 0;
 
    // Stores frequency of
    // distinct elements of array
    HashMap mp = new HashMap<>();
 
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
         
        // Increment the frequency
        // of arr[i]
        mp.put(arr[i],
               mp.getOrDefault(arr[i], 0) + 1);
    }
 
    // Traverse map
    for(Map.Entry freq : mp.entrySet())
    {
        cntPairs += (freq.getValue() *
                    (freq.getValue() - 1)) / 2;
    }
 
    return cntPairs;
}
 
// Driver Code
public static void main(String[] args)
{
    int[] arr = { 1, 2, 3, 1, 2, 2 };
    int N = arr.length;
     
    System.out.println(countPairs(arr, N));
}
}
 
// This code is contributed by akhilsaini


Python3
# Python3 program to implement
# the above approach
 
# Function to count pairs in an array
# whose bitwise AND equal to bitwise OR
def countPairs(arr, N):
     
    # Store count of pairs whose
    # bitwise AND equal to bitwise OR
    cntPairs = 0
 
    # Stores frequency of
    # distinct elements of array
    mp = {}
 
    # Traverse the array
    for i in range(0, N):
         
        # Increment the frequency
        # of arr[i]
        if arr[i] in mp:
            mp[arr[i]] = mp[arr[i]] + 1
        else:
            mp[arr[i]] = 1
 
    # Traverse map
    for freq in mp:
        cntPairs += int((mp[freq] *
                        (mp[freq] - 1)) / 2)
 
    return cntPairs
 
# Driver Code
if __name__ == "__main__":
 
    arr = [ 1, 2, 3, 1, 2, 2 ]
    N = len(arr)
     
    print(countPairs(arr, N))
 
# This code is contributed by akhilsaini


C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to count pairs in an array
// whose bitwise AND equal to bitwise OR
static int countPairs(int[] arr, int N)
{
     
    // Store count of pairs whose
    // bitwise AND equal to bitwise OR
    int cntPairs = 0;
 
    // Stores frequency of
    // distinct elements of array
    Dictionary mp = new Dictionary();
 
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
         
        // Increment the frequency
        // of arr[i]
        if (!mp.ContainsKey(arr[i]))
            mp.Add(arr[i], 1);
        else
            mp[arr[i]] = mp[arr[i]] + 1;
    }
 
    // Traverse map
    foreach(KeyValuePair freq in mp)
    {
        cntPairs += (freq.Value *
                    (freq.Value - 1)) / 2;
    }
 
    return cntPairs;
}
 
// Driver Code
public static void Main()
{
    int[] arr = { 1, 2, 3, 1, 2, 2 };
    int N = arr.Length;
     
    Console.WriteLine(countPairs(arr, N));
}
}
 
// This code is contributed by akhilsaini


输出:
4




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