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

📅  最后修改于: 2021-04-29 03:45:23             🧑  作者: Mango

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

例子:

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

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

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

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

  1. 初始化一个变量,例如res ,以存储满足给定条件的对数。
  2. 遍历给定的数组。
  3. 存储给定数组中每个元素的最高有效位的位置。
  4. 最后,通过上述公式评估结果并打印结果。

下面是上述方法的实现:

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;
    }
    res = (N * (N - 1)) / 2 - res;
 
    return res;
}
 
// Driver Code
int main()
{
 
    int arr[] = { 1, 2, 3, 4, 5, 6 };
    int N = sizeof(arr) / sizeof(arr[0]);
    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;
    }
    res = (N * (N - 1)) / 2 - res;
 
    return res;
}
 
// Driver Code
public static void main(String[] args)
{
    int[] arr = { 1, 2, 3, 4, 5, 6 };
    int N = arr.length;
     
    System.out.println(cntPairs(arr, N));
}
}
 
// This code is contributed by akhilsaini


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(0, N):
         
        # Stores the index of
        # MSB of array elements
        pos = int(math.log(arr[i], 2))
        bit[pos] = bit[pos] + 1
     
    # Calculate number of pairs
    for i in range(0, 32):
        res = res + int((bit[i] *
                        (bit[i] - 1)) / 2)
                         
    res = int((N * (N - 1)) / 2 - res)
     
    return res
 
# Driver Code
if __name__ == "__main__":
     
    arr = [ 1, 2, 3, 4, 5, 6 ]
    N = len(arr)
     
    print(cntPairs(arr, N))
 
# This code is contributed by akhilsaini


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;
    }
    res = (N * (N - 1)) / 2 - res;
 
    return res;
}
 
// Driver Code
public static void Main()
{
    int[] arr = { 1, 2, 3, 4, 5, 6 };
    int N = arr.Length;
     
    Console.Write(cntPairs(arr, N));
}
}
 
// This code is contributed by akhilsaini


Javascript


C++
// C++ program for the above approach
#include 
using namespace std;
 
int findCount(int arr[], int N)
{
    // For storing number of pairs
    int ans = 0;
 
    // For storing count of numbers
    int bits[32] = { 0 };
 
    // Iterate from 0 to N - 1
    for (int i = 0; i < N; i++) {
 
        // Find the most significant bit
        int val = log2l(arr[i]);
 
        ans += bits[val];
        bits[val]++;
    }
    return N * (N - 1) / 2 - ans;
}
 
// Driver Code
int main()
{
    // Given array arr[]
    int arr[] = { 1, 2, 3, 4, 5, 6 };
 
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    cout << findCount(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
static int findCount(int arr[], int N)
{
     
    // For storing number of pairs
    int ans = 0;
 
    // For storing count of numbers
    int bits[] = new int[32];
 
    // Iterate from 0 to N - 1
    for(int i = 0; i < N; i++)
    {
         
        // Find the most significant bit
        int val = (int)(Math.log(arr[i]) /
                        Math.log(2));
 
        ans += bits[val];
        bits[val]++;
    }
    return N * (N - 1) / 2 - ans;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given array arr[]
    int arr[] = { 1, 2, 3, 4, 5, 6 };
 
    int N = arr.length;
 
    // Function Call
    System.out.println(findCount(arr, N));
}
}
 
// This code is contributed by Kingash


Python3
# Python3 program for the above approach
import math
def findCount(arr, N):
 
    # For storing number of pairs
    ans = 0
 
    # For storing count of numbers
    bits = [0] * 32
 
    # Iterate from 0 to N - 1
    for i in range(N):
 
        # Find the most significant bit
        val = int(math.log2(arr[i]))
 
        ans += bits[val]
        bits[val] += 1
    return (N * (N - 1) // 2 - ans)
 
# Driver Code
if __name__ == "__main__":
 
    # Given array arr[]
    arr = [1, 2, 3, 4, 5, 6]
 
    N = len(arr)
 
    # Function Call
    print(findCount(arr, N))
 
    # This code is contributed by ukasp.


C#
// C# program for the above approach
using System;
 
class GFG{
 
static int findCount(int[] arr, int N)
{
     
    // For storing number of pairs
    int ans = 0;
 
    // For storing count of numbers
    int[] bits = new int[32];
 
    // Iterate from 0 to N - 1
    for(int i = 0; i < N; i++)
    {
         
        // Find the most significant bit
        int val = (int)(Math.Log(arr[i]) /
                        Math.Log(2));
 
        ans += bits[val];
        bits[val]++;
    }
    return N * (N - 1) / 2 - ans;
}
 
// Driver Code
public static void Main()
{
     
    // Given array arr[]
    int[] arr = { 1, 2, 3, 4, 5, 6 };
 
    int N = arr.Length;
 
    // Function Call
    Console.Write(findCount(arr, N));
}
}
 
// This code is contributed by subhammahato348


输出:
11

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

方法2:当且仅当最高有效位相等时,才按位和大于按位xor。

  • 创建一个大小为32的bits []数组(最大位数)
  • ans初始化为0。
  • 我们将从头开始遍历数组,对于每个数字,
    • 找到它的最高有效位并说它是j。
    • 将存储在bits [j]数组中的值添加到ans 。 (对于当前元素,可以形成bits [j]对的数量)
    • 现在,将bits [j]的值增加1
  • 现在对总数= n *(n-1)/ 2 。从中减去ans

C++

// C++ program for the above approach
#include 
using namespace std;
 
int findCount(int arr[], int N)
{
    // For storing number of pairs
    int ans = 0;
 
    // For storing count of numbers
    int bits[32] = { 0 };
 
    // Iterate from 0 to N - 1
    for (int i = 0; i < N; i++) {
 
        // Find the most significant bit
        int val = log2l(arr[i]);
 
        ans += bits[val];
        bits[val]++;
    }
    return N * (N - 1) / 2 - ans;
}
 
// Driver Code
int main()
{
    // Given array arr[]
    int arr[] = { 1, 2, 3, 4, 5, 6 };
 
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    cout << findCount(arr, N);
 
    return 0;
}

Java

// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
static int findCount(int arr[], int N)
{
     
    // For storing number of pairs
    int ans = 0;
 
    // For storing count of numbers
    int bits[] = new int[32];
 
    // Iterate from 0 to N - 1
    for(int i = 0; i < N; i++)
    {
         
        // Find the most significant bit
        int val = (int)(Math.log(arr[i]) /
                        Math.log(2));
 
        ans += bits[val];
        bits[val]++;
    }
    return N * (N - 1) / 2 - ans;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given array arr[]
    int arr[] = { 1, 2, 3, 4, 5, 6 };
 
    int N = arr.length;
 
    // Function Call
    System.out.println(findCount(arr, N));
}
}
 
// This code is contributed by Kingash

Python3

# Python3 program for the above approach
import math
def findCount(arr, N):
 
    # For storing number of pairs
    ans = 0
 
    # For storing count of numbers
    bits = [0] * 32
 
    # Iterate from 0 to N - 1
    for i in range(N):
 
        # Find the most significant bit
        val = int(math.log2(arr[i]))
 
        ans += bits[val]
        bits[val] += 1
    return (N * (N - 1) // 2 - ans)
 
# Driver Code
if __name__ == "__main__":
 
    # Given array arr[]
    arr = [1, 2, 3, 4, 5, 6]
 
    N = len(arr)
 
    # Function Call
    print(findCount(arr, N))
 
    # This code is contributed by ukasp.

C#

// C# program for the above approach
using System;
 
class GFG{
 
static int findCount(int[] arr, int N)
{
     
    // For storing number of pairs
    int ans = 0;
 
    // For storing count of numbers
    int[] bits = new int[32];
 
    // Iterate from 0 to N - 1
    for(int i = 0; i < N; i++)
    {
         
        // Find the most significant bit
        int val = (int)(Math.Log(arr[i]) /
                        Math.Log(2));
 
        ans += bits[val];
        bits[val]++;
    }
    return N * (N - 1) / 2 - ans;
}
 
// Driver Code
public static void Main()
{
     
    // Given array arr[]
    int[] arr = { 1, 2, 3, 4, 5, 6 };
 
    int N = arr.Length;
 
    // Function Call
    Console.Write(findCount(arr, N));
}
}
 
// This code is contributed by subhammahato348
输出
11

时间复杂度: O(N)

空间复杂度: O(32)= O(1)