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

📅  最后修改于: 2021-09-02 07:39:57             🧑  作者: 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


Javascript


输出:
11

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

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

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

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

蟒蛇3

# 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

Javascript


输出
11

时间复杂度: O(N)

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live