📌  相关文章
📜  计算给定数组中按位与超过按位XOR的对

📅  最后修改于: 2021-04-18 02:31:52             🧑  作者: Mango

给定大小为N数组arr [] ,任务是计算给定数组中的对数,以使每对的按位AND(&)大于其按位XOR(^)

例子 :

天真的方法:解决问题的最简单方法是遍历数组从给定数组生成所有可能的对。对于每对,检查其按位与( )是否大于其按位XOR ( ^ )。如果发现为真,则将对数增加1 。最后,打印获得的此类对的计数。

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

高效的方法:要优化上述方法,请遵循按位运算符的属性:

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

  1. 初始化一个变量,例如res ,以存储满足给定条件的对数。
  2. 遍历给定数组arr []
  3. 存储给定数组中每个元素的最高有效位( MSB )的位置。
  4. 初始化大小为32 (最大位数)的数组bits []
  5. 遍历每个数组元素并执行以下步骤:
    1. 找到当前数组元素的最高有效位( MSB ) ,例如j
    2. 将存储在位[j]中的值添加到答案中。
    3. 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)