📜  按位 XOR 值大于其按位与值的对的计数 |设置 2

📅  最后修改于: 2022-05-13 01:56:09.223000             🧑  作者: Mango

按位 XOR 值大于其按位与值的对的计数 |设置 2

给定一个包含N个正整数的数组arr 。查找按位XOR值大于按位AND值的所有可能对的计数

例子

Naive Approach:关于解决这个问题的蛮力方法,请参考本文的SET 1。

时间复杂度:O(N*N)
辅助空间:O(1)

有效方法:解决此问题的有效方法基于以下观察:

观察:

插图:

请按照以下步骤解决此问题:

  • 构造一个 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)