给定大小为N的数组arr [] ,任务是计算按位XOR大于对中两个元素的对数。
例子:
Input: arr[] = {2, 4, 3}
Output: 2
Explanation: There are only 2 pairs whose Bitwise XOR is greater than both the elements in the pair:
1) (2, 4): Bitwise XOR = 2 ^ 4 = 6, which is greater than both 2 and 4.
2) (4, 3): Bitwise XOR = 4 ^ 3 = 7, which is greater than both 4 and 3.
Input: arr[] = {2, 2, 2}
Output: 0
Explanation:Since all array elements are the same, Bitwise XOR of all possible pairs is 0. Therefore, no such pair exists
方法:最简单的方法是从给定的数组中生成所有可能的对,并对那些按位XOR大于两个元素的对进行计数。仅检查一次所有对之后,打印计数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function that counts the pairs whose
// Bitwise XOR is greater than both
// the elements of pair
void countPairs(int A[], int N)
{
// Stores the count of pairs
int count = 0;
// Generate all possible pairs
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
// Find the Bitwise XOR
int xo = (A[i] ^ A[j]);
// Find the maximum of two
int mx = max(A[i], A[j]);
// If xo < mx, increment count
if (xo > mx) {
count++;
}
}
}
// Print the value of count
cout << count;
}
// Driver Code
int main()
{
int arr[] = { 2, 4, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
countPairs(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG{
// Function that counts the pairs whose
// Bitwise XOR is greater than both
// the elements of pair
static void countPairs(int[] A, int N)
{
// Stores the count of pairs
int count = 0;
// Generate all possible pairs
for(int i = 0; i < N; i++)
{
for(int j = i + 1; j < N; j++)
{
// Find the Bitwise XOR
int xo = (A[i] ^ A[j]);
// Find the maximum of two
int mx = Math.max(A[i], A[j]);
// If xo < mx, increment count
if (xo > mx)
{
count++;
}
}
}
// Print the value of count
System.out.println(count);
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 2, 4, 3 };
int N = arr.length;
// Function Call
countPairs(arr, N);
}
}
// This code is contributed by akhilsaini
Python3
# Python3 program for the above approach
# Function that counts the pairs whose
# Bitwise XOR is greater than both
# the elements of pair
def countPairs(A, N):
# Stores the count of pairs
count = 0
# Generate all possible pairs
for i in range(0, N):
for j in range(i + 1, N):
# Find the Bitwise XOR
xo = (A[i] ^ A[j])
# Find the maximum of two
mx = max(A[i], A[j])
# If xo < mx, increment count
if (xo > mx):
count += 1
# Print the value of count
print(count)
# Driver Code
if __name__ == '__main__':
arr = [ 2, 4, 3 ]
N = len(arr)
# Function Call
countPairs(arr, N)
# This code is contributed by akhilsaini
C#
// C# program for the above approach
using System;
class GFG{
// Function that counts the pairs whose
// Bitwise XOR is greater than both
// the elements of pair
static void countPairs(int[] A, int N)
{
// Stores the count of pairs
int count = 0;
// Generate all possible pairs
for(int i = 0; i < N; i++)
{
for(int j = i + 1; j < N; j++)
{
// Find the Bitwise XOR
int xo = (A[i] ^ A[j]);
// Find the maximum of two
int mx = Math.Max(A[i], A[j]);
// If xo < mx, increment count
if (xo > mx)
{
count++;
}
}
}
// Print the value of count
Console.WriteLine(count);
}
// Driver Code
public static void Main()
{
int[] arr = { 2, 4, 3 };
int N = arr.Length;
// Function Call
countPairs(arr, N);
}
}
// This code is contributed by akhilsaini
Javascript
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count pairs whose XOR
// is greater than the pair itself
void countPairs(int A[], int N)
{
// Stores the count of pairs
int count = 0;
// Sort the array
sort(A, A + N);
int bits[32] = { 0 };
// Traverse the array
for (int i = 0; i < N; i++) {
// If current element is 0,
// then ignore it
if (A[i] == 0) {
continue;
}
// Traverse all the bits of
// element A[i]
for (int j = 0; j < 32; j++) {
// If current bit is set
// then update the count
if (!((1LL << j) & A[i])) {
count += bits[j];
}
}
// Update bits[] at the most
// significant bit of A[i]
++bits[(int)(log2l(A[i]))];
}
// Print the count
cout << count;
}
// Driver Code
int main()
{
int arr[] = { 2, 4, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
countPairs(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG{
// Function to count pairs whose XOR
// is greater than the pair itself
static void countPairs(int[] A, int N)
{
// Stores the count of pairs
int count = 0;
// Sort the array
Arrays.sort(A);
int[] bits = new int[32];
// Traverse the array
for(int i = 0; i < N; i++)
{
// If current element is 0,
// then ignore it
if (A[i] == 0)
{
continue;
}
// Traverse all the bits of
// element A[i]
for(int j = 0; j < 32; j++)
{
// If current bit is set
// then update the count
if (((1 << j) & A[i]) == 0)
{
count += bits[j];
}
}
// Update bits[] at the most
// significant bit of A[i]
++bits[(int)((int)(Math.log(A[i]) /
Math.log(2)))];
}
// Print the count
System.out.println(count);
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 2, 4, 3 };
int N = arr.length;
// Function Call
countPairs(arr, N);
}
}
// This code is contributed by akhilsaini
Python3
# Python3 program for the above approach
import math
# Function to count pairs whose XOR
# is greater than the pair itself
def countPairs(A, N):
# Stores the count of pairs
count = 0
# Sort the array
A.sort()
bits = [0] * 32
# Traverse the array
for i in range(0, N):
# If current element is 0,
# then ignore it
if (A[i] == 0):
continue
# Traverse all the bits of
# element A[i]
for j in range(0, 32):
# If current bit is set
# then update the count
if (((1 << j) & A[i]) == 0):
count += bits[j]
# Update bits[] at the most
# significant bit of A[i]
bits[(int)(math.log(A[i], 2))] += 1
# Print the count
print(count)
# Driver Code
if __name__ == '__main__':
arr = [ 2, 4, 3 ]
N = len(arr)
# Function Call
countPairs(arr, N)
# This code is contributed by akhilsaini
C#
// C# program for the above approach
using System;
class GFG{
// Function to count pairs whose XOR
// is greater than the pair itself
static void countPairs(int[] A, int N)
{
// Stores the count of pairs
int count = 0;
// Sort the array
Array.Sort(A);
int[] bits = new int[32];
// Traverse the array
for(int i = 0; i < N; i++)
{
// If current element is 0,
// then ignore it
if (A[i] == 0)
{
continue;
}
// Traverse all the bits of
// element A[i]
for(int j = 0; j < 32; j++)
{
// If current bit is set
// then update the count
if (((1 << j) & A[i]) == 0)
{
count += bits[j];
}
}
// Update bits[] at the most
// significant bit of A[i]
++bits[(int)((int)(Math.Log(A[i]) /
Math.Log(2)))];
}
// Print the count
Console.WriteLine(count);
}
// Driver Code
public static void Main()
{
int[] arr = { 2, 4, 3 };
int N = arr.Length;
// Function Call
countPairs(arr, N);
}
}
// This code is contributed by akhilsaini
2
时间复杂度: O(N 2 )
辅助空间: O(1)
高效方法:为了优化上述方法,我们的想法是使用位操作。考虑X = A ^ B和A 和设K是数A的最高有效位现在,如果X是比元件A和B两者,当且仅当K个B的第i位是0时。如果K个整数的第i位是0已经,然后计算,使得第K位的MSB位的其它整数的数字。步骤如下:
- 初始化变量计数以存储对的计数和一个数组,该数组的大小为32 [bits] ,然后对给定的数组进行排序。
- 遍历数组 并执行以下操作:
- 如果当前元素为0,则检查下一个元素。
- 否则,遍历当前元素的所有位,并且如果在位置j处设置了任何位,则将计数增加bit [j] 。
- 完成上述步骤后,将bit [log 2 (当前元素)]更新为1 。
- 完成上述步骤后,将计数值打印为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count pairs whose XOR
// is greater than the pair itself
void countPairs(int A[], int N)
{
// Stores the count of pairs
int count = 0;
// Sort the array
sort(A, A + N);
int bits[32] = { 0 };
// Traverse the array
for (int i = 0; i < N; i++) {
// If current element is 0,
// then ignore it
if (A[i] == 0) {
continue;
}
// Traverse all the bits of
// element A[i]
for (int j = 0; j < 32; j++) {
// If current bit is set
// then update the count
if (!((1LL << j) & A[i])) {
count += bits[j];
}
}
// Update bits[] at the most
// significant bit of A[i]
++bits[(int)(log2l(A[i]))];
}
// Print the count
cout << count;
}
// Driver Code
int main()
{
int arr[] = { 2, 4, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
countPairs(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG{
// Function to count pairs whose XOR
// is greater than the pair itself
static void countPairs(int[] A, int N)
{
// Stores the count of pairs
int count = 0;
// Sort the array
Arrays.sort(A);
int[] bits = new int[32];
// Traverse the array
for(int i = 0; i < N; i++)
{
// If current element is 0,
// then ignore it
if (A[i] == 0)
{
continue;
}
// Traverse all the bits of
// element A[i]
for(int j = 0; j < 32; j++)
{
// If current bit is set
// then update the count
if (((1 << j) & A[i]) == 0)
{
count += bits[j];
}
}
// Update bits[] at the most
// significant bit of A[i]
++bits[(int)((int)(Math.log(A[i]) /
Math.log(2)))];
}
// Print the count
System.out.println(count);
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 2, 4, 3 };
int N = arr.length;
// Function Call
countPairs(arr, N);
}
}
// This code is contributed by akhilsaini
Python3
# Python3 program for the above approach
import math
# Function to count pairs whose XOR
# is greater than the pair itself
def countPairs(A, N):
# Stores the count of pairs
count = 0
# Sort the array
A.sort()
bits = [0] * 32
# Traverse the array
for i in range(0, N):
# If current element is 0,
# then ignore it
if (A[i] == 0):
continue
# Traverse all the bits of
# element A[i]
for j in range(0, 32):
# If current bit is set
# then update the count
if (((1 << j) & A[i]) == 0):
count += bits[j]
# Update bits[] at the most
# significant bit of A[i]
bits[(int)(math.log(A[i], 2))] += 1
# Print the count
print(count)
# Driver Code
if __name__ == '__main__':
arr = [ 2, 4, 3 ]
N = len(arr)
# Function Call
countPairs(arr, N)
# This code is contributed by akhilsaini
C#
// C# program for the above approach
using System;
class GFG{
// Function to count pairs whose XOR
// is greater than the pair itself
static void countPairs(int[] A, int N)
{
// Stores the count of pairs
int count = 0;
// Sort the array
Array.Sort(A);
int[] bits = new int[32];
// Traverse the array
for(int i = 0; i < N; i++)
{
// If current element is 0,
// then ignore it
if (A[i] == 0)
{
continue;
}
// Traverse all the bits of
// element A[i]
for(int j = 0; j < 32; j++)
{
// If current bit is set
// then update the count
if (((1 << j) & A[i]) == 0)
{
count += bits[j];
}
}
// Update bits[] at the most
// significant bit of A[i]
++bits[(int)((int)(Math.Log(A[i]) /
Math.Log(2)))];
}
// Print the count
Console.WriteLine(count);
}
// Driver Code
public static void Main()
{
int[] arr = { 2, 4, 3 };
int N = arr.Length;
// Function Call
countPairs(arr, N);
}
}
// This code is contributed by akhilsaini
2
时间复杂度: O(N * log N)
辅助空间: O(1)