给定一个由n个正整数和一个整数K组成的数组arr [] ,任务是计算给定数组的所有可能的对,其按位OR等于K。
例子:
Input: arr[] = {2, 38, 44, 29, 62}, K = 46
Output: 2
Explanation: Only the following two pairs are present in the array whose Bitwise OR is 46:
- 2 OR 44 = 46
- 38 OR 44 = 46
Input: arr[] = {1, 5, 20, 15, 14}, K = 20
Output: 5
Explanation:
There are only 5 pairs whose Bitwise OR is 20:
- 1 OR 15 = 15
- 1 OR 14 = 15
- 5 OR 15 = 15
- 5 OR 14 = 15
- 15 OR 14 = 15
方法:解决该问题的想法是从给定的数组中生成所有可能的对,并对那些按位或等于K的对进行计数。检查所有对之后,打印存储的计数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function that counts the pairs from
// the array whose Bitwise OR is K
void countPairs(int arr[], int k, int size)
{
// Stores the required
// count of pairs
int count = 0, x;
// Generate all possible pairs
for (int i = 0; i < size - 1; i++) {
for (int j = i + 1; j < size; j++) {
// Perform OR operation
x = arr[i] | arr[j];
// If Bitwise OR is equal
// to K, increment count
if (x == k)
count++;
}
}
// Print the total count
cout << count;
}
// Driver Code
int main()
{
int arr[] = { 2, 38, 44, 29, 62 };
int K = 46;
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
countPairs(arr, K, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG{
// Function that counts the pairs from
// the array whose Bitwise OR is K
static void countPairs(int[] arr, int k,
int size)
{
// Stores the required
// count of pairs
int count = 0, x;
// Generate all possible pairs
for(int i = 0; i < size - 1; i++)
{
for(int j = i + 1; j < size; j++)
{
// Perform OR operation
x = arr[i] | arr[j];
// If Bitwise OR is equal
// to K, increment count
if (x == k)
count++;
}
}
// Print the total count
System.out.println(count);
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 2, 38, 44, 29, 62 };
int K = 46;
int N = arr.length;
// Function Call
countPairs(arr, K, N);
}
}
// This code is contributed by code_hunt
Python3
# Python3 program for the above approach
# Function that counts the pairs from
# the array whose Bitwise OR is K
def countPairs(arr, k, size):
# Stores the required
# count of pairs
count = 0
# Generate all possible pairs
for i in range(size - 1):
for j in range(i + 1, size):
# Perform OR operation
x = arr[i] | arr[j]
# If Bitwise OR is equal
# to K, increment count
if (x == k):
count += 1
# Print the total count
print(count)
# Driver Code
arr = [ 2, 38, 44, 29, 62 ]
K = 46
N = len(arr)
# Function Call
countPairs(arr, K, N)
# This code is contributed by sanjoy_62
C#
// C# program for the above approach
using System;
class GFG{
// Function that counts the pairs from
// the array whose Bitwise OR is K
static void countPairs(int[] arr, int k,
int size)
{
// Stores the required
// count of pairs
int count = 0, x;
// Generate all possible pairs
for(int i = 0; i < size - 1; i++)
{
for(int j = i + 1; j < size; j++)
{
// Perform OR operation
x = arr[i] | arr[j];
// If Bitwise OR is equal
// to K, increment count
if (x == k)
count++;
}
}
// Print the total count
Console.WriteLine(count);
}
// Driver Code
public static void Main()
{
int[] arr = { 2, 38, 44, 29, 62 };
int K = 46;
int N = arr.Length;
// Function Call
countPairs(arr, K, N);
}
}
// This code is contributed by susmitakundugoaldanga
Javascript
输出:
2
时间复杂度: O(N 2 )
辅助空间: O(1)