
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count Pairs Whose Bitwise AND Exceeds Bitwise XOR from a Given Array
In this article, we will learn how to count pairs of numbers from a given array where the bitwise AND of the two numbers is greater than the bitwise XOR. Bitwise operations work on the individual bits of numbers. The AND operation results in 1 only when both bits are 1, while XOR gives 1 when the bits are different.
Given an array of integers, we need to count how many pairs (i, j) exist such that the bitwise AND of the pair is greater than the bitwise XOR. In other words, for each pair of elements in the array, we check if:
AND(arr[i], arr[j]) > XOR(arr[i], arr[j])
Where arr[i] and arr[j] are two distinct integers in the array.
Example
For the array arr = [2, 3, 5, 7], let's check each pair:
- Pair (2, 3): ? Bitwise AND: 2 & 3 = 2, Bitwise XOR: 2 ^ 3 = 1 ? AND > XOR (Valid pair).
- Pair (2, 5): ? Bitwise AND: 2 & 5 = 0, Bitwise XOR: 2 ^ 5 = 7 ? AND <= XOR (Not valid).
- Pair (2, 7): ? Bitwise AND: 2 & 7 = 2, Bitwise XOR: 2 ^ 7 = 5 ? AND > XOR (Valid pair).
- Pair (3, 5): ? Bitwise AND: 3 & 5 = 1, Bitwise XOR: 3 ^ 5 = 6 ? AND <= XOR (Not valid).
- Pair (3, 7): ? Bitwise AND: 3 & 7 = 3, Bitwise XOR: 3 ^ 7 = 4 ? AND > XOR (Valid pair).
- Pair (5, 7): ? Bitwise AND: 5 & 7 = 5, Bitwise XOR: 5 ^ 7 = 2 ? AND > XOR (Valid pair).
The valid pairs are: (2, 3), (2, 7), (3, 7), and (5, 7). So, the count of valid pairs is 4.
Constraints
Before moving forward, we need to keep the following constraints in mind:
- The array size can vary from small to large, so an efficient solution is required to handle larger inputs effectively.
- The array will only contain positive integers, simplifying the bitwise operations.
- Comparing all pairs requires O(n^2) time, which may be too slow for large arrays, making an optimized approach is important.
Naive Approach (Brute Force Solution)
The simplest approach is to check every possible pair(i, j) in the array and compare their bitwise AND and XOR. This method is straightforward but inefficient.
Example
In this example, the code checks all pairs (i, j) in the array and counts how many satisfy the condition AND(arr[i], arr[j]) > XOR(arr[i], arr[j]). For the array {5, 10, 15, 20, 25}, the result is the number of valid pairs where this condition holds true.
#include <iostream> #include <vector> using namespace std; int countPairs(const vector<int>& arr) { int count = 0; int n = arr.size(); // Loop through every pair (i, j) where i != j for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { // Skip same index pairs if (i == j) continue; // Check the condition: AND > XOR if ((arr[i] & arr[j]) > (arr[i] ^ arr[j])) { count++; } } } return count; } int main() { vector<int> arr = {5, 10, 15, 20, 25}; // Example array int result = countPairs(arr); cout << "Number of pairs where AND exceeds XOR: " << result << endl; return 0; }
The output shows that there are 4 pairs where the bitwise AND exceeds the bitwise XOR.
Number of pairs where AND exceeds XOR: 4
Time Complexity: O(n^2), we loop through all pairs of elements, making the time complexity quadratic.
Space Complexity: O(1), we only use a constant amount of space for variables like count.
Optimized Approach
The naive approach has a time complexity of O(n^2), which is inefficient for large arrays. To optimize:
- We avoid redundant pair checks by considering symmetric pairs. If we've already checked (arr[i], arr[j]), we don't need to check (arr[j], arr[i]) since the results will be the same. This reduces the number of checks by half.
- For pairs (arr[i], arr[j]), the bitwise condition should be checked only once for each unique pair where i < j.
Example
Here's a complete code where we loop through each unique pair (i, j) where i < j, checking if AND(arr[i], arr[j]) > XOR(arr[i], arr[j]). We also account for symmetric pairs (j, i) to avoid redundant checks and improve efficiency.
#include <iostream> #include <vector> using namespace std; int countPairs(vector<int>& arr) { int count = 0; int n = arr.size(); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if ((arr[i] & arr[j]) > (arr[i] ^ arr[j])) { count++; } // Symmetric pairs if ((arr[j] & arr[i]) > (arr[j] ^ arr[i])) { count++; } } } return count; } int main() { vector<int> arr = {5, 10, 15, 20, 25}; int result = countPairs(arr); cout << "Number of pairs where AND exceeds XOR: " << result << endl; return 0; }
The below output shows the total number of pairs where the bitwise AND exceeds the bitwise XOR:
Number of pairs where AND exceeds XOR: 4
Time Complexity: O(n^2), the optimization reduces the redundant checks but doesn't change the overall time complexity since we're still checking all pairs.
Space Complexity: O(1), we still use only a constant amount of extra space.
Conclusion
In this article, we discussed how to count the number of pairs in an array where the bitwise AND exceeds the bitwise XOR. We covered both a brute-force approach and an optimized solution. By considering symmetric pairs and avoiding redundant checks, we improved the performance for larger arrays.