
- 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
Count all pairs with given XOR in C++
In this tutorial, we will be discussing a program to find the number of pairs with the given XOR.
For this we will be provided with an array and a value. Our task is to find the number of pairs whose XOR is equal to the given value.
Example
#include<bits/stdc++.h> using namespace std; //returning the number of pairs //having XOR equal to given value int count_pair(int arr[], int n, int x){ int result = 0; //managing with duplicate values unordered_map<int, int> m; for (int i=0; i<n ; i++){ int curr_xor = x^arr[i]; if (m.find(curr_xor) != m.end()) result += m[curr_xor]; m[arr[i]]++; } return result; } int main(){ int arr[] = {2, 5, 2}; int n = sizeof(arr)/sizeof(arr[0]); int x = 0; cout << "Count of pairs with given XOR = " << count_pair(arr, n, x); return 0; }
Output
Count of pairs with given XOR = 1
- Related Questions & Answers
- Count pairs with Odd XOR in C++
- Count pairs with Bitwise XOR as EVEN number in C++
- Count pairs with Bitwise XOR as ODD number in C++
- Count pairs with given sum in C++
- Print all pairs with given sum in C++
- Count all pairs of adjacent nodes whose XOR is an odd number in C++
- Program to count pairs with XOR in a range in Python
- Sum of XOR of all pairs in an array in C++
- Count all distinct pairs with difference equal to k in C++
- Find all the pairs with given sum in a BST in C++
- Sum of XOR of sum of all pairs in an array in C++
- Count pairs of natural numbers with GCD equal to given number in C++
- Count index pairs which satisfy the given condition in C++
- Count valid pairs in the array satisfying given conditions in C++
- Count the pairs of vowels in the given string in C++
Advertisements