给定N个正整数的数组arr [] 。任务是找到按位与值是2的幂的对的数量。
例子:
Input: arr[] = {2, 1, 3, 4}
Output: 2
Explanation:
There are 2 pairs (2, 3) and (1, 3) in this array whose Bitwise AND values are:
1. (2 & 3) = 1 = (20)
2. (1 & 3) = 1 = (20).
Input: arr[] = {6, 4, 2, 3}
Output: 4
Explanation:
There are 4 pairs (6, 4), (6, 2), (6, 3), (2, 3) whose Bitwise and is power of 2.
方法:对于给定数组中的每个可能的对,检查每对元素的按位与的想法是否为2的乘方幂。如果“是”,则计算该对,否则检查下一对。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if x is power of 2
bool check(int x)
{
// Returns true if x is a power of 2
return x && (!(x & (x - 1)));
}
// Function to return the
// number of valid pairs
int count(int arr[], int n)
{
int cnt = 0;
// Iterate for all possible pairs
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
// Bitwise and value of
// the pair is passed
if (check(arr[i]
& arr[j]))
cnt++;
}
}
// Return the final count
return cnt;
}
// Driver Code
int main()
{
// Given array
int arr[] = { 6, 4, 2, 3 };
int n = sizeof(arr) / sizeof(arr[0]);
// Function Call
cout << count(arr, n);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Method to check if x is power of 2
static boolean check(int x)
{
// First x in the below expression
// is for the case when x is 0
return x != 0 && ((x & (x - 1)) == 0);
}
// Function to return the
// number of valid pairs
static int count(int arr[], int n)
{
int cnt = 0;
// Iterate for all possible pairs
for(int i = 0; i < n - 1; i++)
{
for(int j = i + 1; j < n; j++)
{
// Bitwise and value of
// the pair is passed
if (check(arr[i] & arr[j]))
cnt++;
}
}
// Return the final count
return cnt;
}
// Driver Code
public static void main(String[] args)
{
// Given array arr[]
int arr[] = new int[]{ 6, 4, 2, 3 };
int n = arr.length;
// Function call
System.out.print(count(arr, n));
}
}
// This code is contributed by Pratima Pandey
Python3
# Python3 program for the above approach
# Function to check if x is power of 2
def check(x):
# Returns true if x is a power of 2
return x and (not(x & (x - 1)))
# Function to return the
# number of valid pairs
def count(arr, n):
cnt = 0
# Iterate for all possible pairs
for i in range(n - 1):
for j in range(i + 1, n):
# Bitwise and value of
# the pair is passed
if check(arr[i] & arr[j]):
cnt = cnt + 1
# Return the final count
return cnt
# Given array
arr = [ 6, 4, 2, 3 ]
n = len(arr)
# Function Call
print(count(arr, n))
# This code is contributed by divyeshrabadiya07
C#
// C# program for the above approach
using System;
class GFG{
// Method to check if x is power of 2
static bool check(int x)
{
// First x in the below expression
// is for the case when x is 0
return x != 0 && ((x & (x - 1)) == 0);
}
// Function to return the
// number of valid pairs
static int count(int []arr, int n)
{
int cnt = 0;
// Iterate for all possible pairs
for(int i = 0; i < n - 1; i++)
{
for(int j = i + 1; j < n; j++)
{
// Bitwise and value of
// the pair is passed
if (check(arr[i] & arr[j]))
cnt++;
}
}
// Return the final count
return cnt;
}
// Driver Code
public static void Main()
{
// Given array arr[]
int []arr = new int[]{ 6, 4, 2, 3 };
int n = arr.Length;
// Function call
Console.Write(count(arr, n));
}
}
// This code is contributed by Code_Mech
输出:
4
时间复杂度: O(N 2 )
辅助空间: O(1)