给定大小为N的数组arr [] ,任务是计算给定数组中的对数,以使每对的按位AND(&)大于其按位XOR(^) 。
例子 :
Input: arr[] = {1, 2, 3, 4}
Output:1
Explanation:
Pairs that satisfy the given conditions are:
(2 & 3) > (2 ^ 3)
Therefore, the required output is 1.
Input: arr[] = {1, 4, 3, 7}
Output: 1
Explanation:
Pairs that satisfy the given conditions are:
(4 & 7) > (4 ^ 3)
Therefore, the required output is 1.
天真的方法:解决问题的最简单方法是遍历数组并从给定数组生成所有可能的对。对于每对,检查其按位与( & )是否大于其按位XOR ( ^ )。如果发现为真,则将对数增加1 。最后,打印获得的此类对的计数。
时间复杂度: O(N 2 )
辅助空间: O(1)
高效的方法:要优化上述方法,请遵循按位运算符的属性:
1 ^ 0 = 1
0 ^ 1 = 1
1 & 1 = 1
X = b31b30…..b1b0
Y = a31b30….a1a0
If the expression {(X & Y) > (X ^ Y)} is true, then the Most Significant Bit (MSB) of both X and Y must be equal.
Total count of pairs that satisfying the condition {(X & Y) > (X ^ Y)} is equal to:
请按照以下步骤解决问题:
- 初始化一个变量,例如res ,以存储满足给定条件的对数。
- 遍历给定数组arr [] 。
- 存储给定数组中每个元素的最高有效位( MSB )的位置。
- 初始化大小为32 (最大位数)的数组bits []
- 遍历每个数组元素并执行以下步骤:
- 找到当前数组元素的最高有效位( MSB ) ,例如j 。
- 将存储在位[j]中的值添加到答案中。
- 将bits [j]的值增加1 。
下面是上述方法的实现
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to count pairs that
// satisfy the above condition
int cntPairs(int arr[], int N)
{
// Stores the count of pairs
int res = 0;
// Stores the count of array
// elements having same
// positions of MSB
int bit[32] = { 0 };
// Traverse the array
for (int i = 0; i < N; i++) {
// Stores the index of
// MSB of array elements
int pos = log2(arr[i]);
bit[pos]++;
}
// Calculate number of pairs
for (int i = 0; i < 32; i++) {
res += (bit[i] * (bit[i] - 1)) / 2;
}
return res;
}
// Driver Code
int main()
{
// Given Input
int arr[] = { 1, 2, 3, 4 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function call to count pairs
// satisfying the given condition
cout << cntPairs(arr, N);
}
Java
// Java program to implement
// the above approach
import java.io.*;
class GFG{
// Function to count pairs that
// satisfy the above condition
static int cntPairs(int arr[], int N)
{
// Stores the count of pairs
int res = 0;
// Stores the count of array
// elements having same
// positions of MSB
int bit[] = new int[32];
// Traverse the array
for(int i = 0; i < N; i++)
{
// Stores the index of
// MSB of array elements
int pos = (int)(Math.log(arr[i]) / Math.log(2));
bit[pos]++;
}
// Calculate number of pairs
for(int i = 0; i < 32; i++)
{
res += (bit[i] * (bit[i] - 1)) / 2;
}
return res;
}
// Driver Code
public static void main(String[] args)
{
// Given Input
int arr[] = { 1, 2, 3, 4 };
int N = arr.length;
// Function call to count pairs
// satisfying the given condition
System.out.println(cntPairs(arr, N));
}
}
// This code is contributed by Dharanendra L V.
Python3
# Python3 program to implement
# the above approach
import math
# Function to count pairs that
# satisfy the above condition
def cntPairs(arr, N):
# Stores the count of pairs
res = 0
# Stores the count of array
# elements having same
# positions of MSB
bit = [0] * 32
# Traverse the array
for i in range(N):
# Stores the index of
# MSB of array elements
pos = (int)(math.log2(arr[i]))
bit[pos] += 1
# Calculate number of pairs
for i in range(32):
res += (bit[i] * (bit[i] - 1)) // 2
return res
# Driver Code
if __name__ == "__main__":
# Given Input
arr = [1, 2, 3, 4]
N = len(arr)
# Function call to count pairs
# satisfying the given condition
print(cntPairs(arr, N))
# This code is contributed by ukasp
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to count pairs that
// satisfy the above condition
static int cntPairs(int[] arr, int N)
{
// Stores the count of pairs
int res = 0;
// Stores the count of array
// elements having same
// positions of MSB
int[] bit = new int[32];
// Traverse the array
for(int i = 0; i < N; i++)
{
// Stores the index of
// MSB of array elements
int pos = (int)(Math.Log(arr[i]) / Math.Log(2));
bit[pos]++;
}
// Calculate number of pairs
for(int i = 0; i < 32; i++)
{
res += (bit[i] * (bit[i] - 1)) / 2;
}
return res;
}
// Driver Code
static public void Main ()
{
// Given Input
int[] arr = { 1, 2, 3, 4 };
int N = arr.Length;
// Function call to count pairs
// satisfying the given condition
Console.Write(cntPairs(arr, N));
}
}
// This code is contributed by avijitmondal1998
1
时间复杂度: O(N)
辅助空间: O(1)