📌  相关文章
📜  来自Array的对的计数,总和等于其按位与的两倍

📅  最后修改于: 2021-04-29 05:13:56             🧑  作者: Mango

给定数组arr [] ,任务是对数组中的对进行计数,其和等于其按位与的两倍。 A + B = 2 * (A \& B)
例子:

天真的方法:一个简单的解决方案是遍历每个可能的对,并检查该对的总和是否等于该对的按位与的两倍。如果该对具有相等的总和并按位与,则将这些对的计数增加1。

高效方法:想法是利用和与按位与之间的关系。那是 –

A + B = (A \^ B) + 2 * (A \& B)

在等于和和按位与的情况下,该对的按位XOR值应等于0。我们知道,只有两对彼此相等时,任何两对的按位XOR才等于0。因此,如果X是元素的频率。然后增加对的数量^{X}C_{2}

下面是上述方法的实现:

C++
// C++ implementation to find the pairs
// with equal sum and twice the
// bitwise AND of the pairs
  
#include 
  
using namespace std;
  
// Map to store the
// occurrence of
// elements of array
map mp;
  
// Function to find the pairs
// with equal sum and twice the
// bitwise AND of the pairs
int find_pairs(int ar[], int n)
{
    int ans = 0;
  
    // Loop to find the frequency
    // of elements of array
    for (int i = 0; i < n; i++) {
        mp[ar[i]]++;
    }
  
    // Function to find the count
    // such pairs in the array
    for (auto i : mp) {
        int count = i.second;
        if (count > 1) {
  
            // if an element occurs more
            // than once then the answer
            // will by incremented
            // by nC2 times
            ans += ((count
                     * (count - 1))
                    / 2);
        }
    }
    return ans;
}
  
// Driver Code
int main()
{
    int ar[]
        = { 1, 2, 3, 3, 4,
            5, 5, 7, 8 };
    int arr_size = (sizeof(ar)
                    / sizeof(ar[0]));
  
    // Function Call
    cout << find_pairs(ar, arr_size);
    return 0;
}


Java
// Java implementation to find the pairs
// with equal sum and twice the
// bitwise AND of the pairs
import java.util.*;
  
class GFG{
  
// Map to store the
// occurrence of
// elements of array
static HashMap mp = new HashMap();
  
// Function to find the pairs
// with equal sum and twice the
// bitwise AND of the pairs
static int find_pairs(int arr[], int n)
{
    int ans = 0;
  
    // Loop to find the frequency
    // of elements of array
    for(int i = 0; i < n; i++)
    {
       if(mp.containsKey(arr[i]))
       {
           mp.put(arr[i], mp.get(arr[i]) + 1);
       }
       else
       {
           mp.put(arr[i], 1);
       }
    }
      
    // Function to find the count
    // such pairs in the array
    for(Map.Entry i:mp.entrySet())
    {
       int count = i.getValue();
       if (count > 1)
       {
  
           // If an element occurs more
           // than once then the answer
           // will by incremented
           // by nC2 times
           ans += ((count * (count - 1)) / 2);
       }
    }
    return ans;
}
  
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 1, 2, 3, 3, 4,
                  5, 5, 7, 8 };
    int arr_size = arr.length;
  
    // Function Call
    System.out.print(find_pairs(arr, arr_size));
}
}
  
// This code is contributed by amal kumar choubey


Python3
# Python3 implementation to find the 
# pairs with equal sum and twice the
# bitwise AND of the pairs
from collections import defaultdict 
  
# Map to store the occurrence 
# of elements of array
mp = defaultdict(int)
  
# Function to find the pairs
# with equal sum and twice the
# bitwise AND of the pairs
def find_pairs(arr, n):
  
    ans = 0
  
    # Loop to find the frequency
    # of elements of array
    for i in range(n):
        mp[arr[i]] += 1
  
    # Function to find the count
    # such pairs in the array
    for i in mp.values():
        count = i
        if (count > 1):
  
            # If an element occurs more
            # than once then the answer
            # will by incremented
            # by nC2 times
            ans += ((count * (count - 1)) // 2)
      
    return ans
  
# Driver Code
if __name__ == "__main__":
  
    arr = [ 1, 2, 3, 3, 4,
            5, 5, 7, 8 ]
    arr_size = len(arr)
  
    # Function Call
    print(find_pairs(arr, arr_size))
  
# This code is contributed by chitranayal


C#
// C# implementation to find the pairs
// with equal sum and twice the
// bitwise AND of the pairs
using System;
using System.Collections.Generic;
  
class GFG{
  
// To store the occurrence 
// of elements of array
static Dictionary mp = new Dictionary();
  
// Function to find the pairs
// with equal sum and twice the
// bitwise AND of the pairs
static int find_pairs(int []arr, int n)
{
    int ans = 0;
  
    // Loop to find the frequency
    // of elements of array
    for(int i = 0; i < n; i++)
    {
       if(mp.ContainsKey(arr[i]))
       {
           mp[arr[i]] = mp[arr[i]] + 1;
       }
       else
       {
           mp.Add(arr[i], 1);
       }
    }
      
    // Function to find the count
    // such pairs in the array
    foreach(KeyValuePair i in mp)
    {
       int count = i.Value;
       if (count > 1)
       {
             
           // If an element occurs more
           // than once then the answer
           // will by incremented
           // by nC2 times
           ans += ((count * (count - 1)) / 2);
       }
    }
    return ans;
}
  
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 1, 2, 3, 3, 4,
                  5, 5, 7, 8 };
    int arr_size = arr.Length;
  
    // Function Call
    Console.Write(find_pairs(arr, arr_size));
}
}
  
// This code is contributed by amal kumar choubey


输出:
2