- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- 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

Suppose we have an array of n elements; we have to find a number of pairs in the array whose XOR will be 0. The pair (x, y) whose XOR is 0, then x = y. To solve it we can sort the array, then if two consecutive elements are the same, increase the count. If all elements are the same, then the last count may not be counted. In that case, we will check whether the last and first elements are the same or not, if the same, then increase the count by 1.

#include<iostream> #include<algorithm> using namespace std; int countPairs(int arr[], int n) { int count = 0; sort(arr, arr+n); for(int i = 0; i<n - 1; i++){ if(arr[i] == arr[i+1]){ count++; } } if(arr[0] == arr[n-1]) count++; return count; } int main() { int arr[] = {1, 2, 1, 2, 4}; int n = sizeof(arr)/sizeof(arr[0]); cout << "Number of pairs: " << countPairs(arr, n); }

Number of pairs: 2

- Related Questions & Answers
- Count ordered pairs of positive numbers such that their sum is S and XOR is K in C++
- Find number of pairs (x, y) in an array such that x^y > y^x in C++
- Find the Number of Prime Pairs in an Array using C++
- Find the Number of Unique Pairs in an Array using C++
- Find all pairs (a, b) in an array such that a % b = k in C++
- Sum of XOR of all pairs in an array in C++
- Find smallest number K such that K % p = 0 and q % K = 0 in C++
- Count pairs in an array such that at least one element is prime in C++
- Find an element in array such that sum of left array is equal to sum of right array using c++
- Count pairs in an array such that frequency of one is at least value of other in C++
- Find prime number K in an array such that (A[i] % K) is maximum in C++
- Count of pairs (x, y) in an array such that x < y in C++
- Count all pairs of adjacent nodes whose XOR is an odd number in C++
- Sum of XOR of sum of all pairs in an array in C++
- Find maximum number of elements such that their absolute difference is less than or equal to 1 in C++

Advertisements