按位 XOR 值大于其按位与值的对的计数 |设置 2
给定一个包含N个正整数的数组arr 。查找按位XOR值大于按位AND值的所有可能对的计数
例子:
Input : arr[]={ 12, 4 , 15}
Output: 2
Explanation: 12 ^ 4 = 8 , 12 & 4 = 4 . so 12 ^ 4 > 12 & 4
4 ^ 15 = 11, 4 & 15 = 4. so 4 ^ 15 > 4 & 15 .
So , above two are valid pairs { 12,4 } ,{4, 15} .
Input: arr[] ={ 1, 1, 3, 3 }
Output: 4
Naive Approach:关于解决这个问题的蛮力方法,请参考本文的SET 1。
时间复杂度:O(N*N)
辅助空间:O(1)
有效方法:解决此问题的有效方法基于以下观察:
观察:
The observation is that if the index of MSB ( Most Significant bit, also known as leftmost set bit), of any two elements is the same then it is guaranteed that the bitwise XOR of those two elements is less than their bitwise AND ., because 1^1=0, it will unset the MSB. while 1&1=1, MSB will remain set.
插图:
If a = 1010, b = 1000,
Then a & b= 1000 and a ^ b = 0010
So if the MSB index is the same in both then their bitwise XOR will always be less than bitwise AND . Vice versa if the MSB index is different then their bitwise XOR value will be greater than the bitwise AND value. Consider the below example to understand this sentence.
请按照以下步骤解决此问题:
- 构造一个 MSB 数组,MSB[ i ] 将表示 MSB 位索引为 i 的元素的计数。
- 从 totat 中删除 (MSB[ i ]*(MSB[ i ]-1))/2 。因为 MSB 相同的对不是有效对。
- 返回有效计数。
以下是上述方法的实现:
C++
// C++ program to Count number of pairs
//whose bit wise XOR value is greater
//than bit wise AND value
#include
using namespace std;
int MSB[32];
// return count of pairs
// whose bitwise xor >= bitwise
long long valid_pairs(int arr[], int N)
{
for (int i = 0; i < N; i++) {
// index of MSB in arr[i]
int MSB_index = log2(arr[i]);
MSB[MSB_index]++;
}
long long tot = (N * (N - 1)) / 2;
long long invalid = 0;
// pairs are invalid if
// their MSB index are same
for (int i = 0; i < 32; i++)
invalid += ((MSB[i] * 1LL * (MSB[i] - 1)) / 2);
long long valid = tot - invalid;
return valid;
}
//Driver Code
int main()
{
int N = 3;
int arr[] = { 12, 4, 15 };
cout << valid_pairs(arr, N);
}
Java
// Java program to Count number of pairs
// whose bit wise XOR value is greater
// than bit wise AND value
import java.io.*;
import java.lang.*;
class GFG
{
// Function to calculate the
// log base 2 of an integer
public static int log2(int N)
{
// calculate log2 N indirectly
// using log() method
int result = (int)(Math.log(N) / Math.log(2));
return result;
}
static int MSB[] = new int[32];
// return count of pairs
// whose bitwise xor >= bitwise
public static long valid_pairs(int arr[], int N)
{
for (int i = 0; i < N; i++) {
// index of MSB in arr[i]
int MSB_index = log2(arr[i]);
MSB[MSB_index]++;
}
long tot = (N * (N - 1)) / 2;
long invalid = 0;
// pairs are invalid if
// their MSB index are same
for (int i = 0; i < 32; i++)
invalid += (((long)MSB[i] * (MSB[i] - 1)) / 2);
long valid = tot - invalid;
return valid;
}
public static void main(String[] args)
{
int N = 3;
int arr[] = { 12, 4, 15 };
System.out.print(valid_pairs(arr, N));
}
}
// This code is contributed by Rohit Pradhan
C#
// C# program to Count number of pairs
// whose bit wise XOR value is greater
// than bit wise AND value
using System;
public class GFG{
// Function to calculate the
// log base 2 of an integer
public static int log2(int N)
{
// calculate log2 N indirectly
// using log() method
int result = (int)(Math.Log(N) / Math.Log(2));
return result;
}
static int[] MSB = new int[32];
// return count of pairs
// whose bitwise xor >= bitwise
public static long valid_pairs(int[] arr, int N)
{
for (int i = 0; i < N; i++) {
// index of MSB in arr[i]
int MSB_index = log2(arr[i]);
MSB[MSB_index]++;
}
long tot = (N * (N - 1)) / 2;
long invalid = 0;
// pairs are invalid if
// their MSB index are same
for (int i = 0; i < 32; i++)
invalid += (((long)MSB[i] * (MSB[i] - 1)) / 2);
long valid = tot - invalid;
return valid;
}
static public void Main (){
int N = 3;
int[] arr = { 12, 4, 15 };
Console.Write(valid_pairs(arr, N));
}
}
// This code is contributed by hrithikgarg03188.
Javascript
2
时间复杂度:O(N)
辅助空间:O(1)