给定一个数组arr[] ,任务是计算数组中总和等于其按位 AND 两倍的对,即,
例子:
Input: arr[] = {1, 1, 3, 4, 4, 5, 7, 8}
Output: 2
Explaination:
Pairs with sum equal to twice their bitwise AND:
{(1, 1), (4, 4)}
Input: arr[] = {1, 3, 3, 5, 4, 6}
Output: 1
朴素的方法:一个简单的解决方案是迭代每个可能的对并检查该对的总和是否等于该对的按位与的两倍。如果该对具有相等的和和按位 AND,则将此类对的计数加 1。
有效的方法:这个想法是使用总和和按位与之间的关系。那是 –
在此等和和按位与中,该对的按位异或的值应等于 0。我们知道,任何两对的按位异或只有在它们彼此相等时才等于 0。因此,如果 X 是元素的频率。然后增加对的计数 .
下面是上述方法的实现:
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
Javascript
输出:
2
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live