
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Count pairs with Bitwise XOR as EVEN number in C++
We are given an integer array and the task is to count the total number of pairs that can be formed using the given array values such that the XOR operation on the pairs will result in an EVEN value.
The truth table for XOR operation is given below
A | B | A XOR B |
0 | 0 | 0 |
1 | 0 | 1 |
0 | 1 | 1 |
1 | 1 | 0 |
Input − int arr[] = {2, 8, 1, 5, 11}
Output − Count of pairs with Bitwise XOR as EVEN number are − 4
Explanation −
a1 | a2 | a1 XOR a2 |
2 | 8 | 10 |
2 | 1 | 3 |
2 | 5 | 7 |
2 | 11 | 9 |
8 | 1 | 9 |
8 | 5 | 13 |
8 | 11 | 3 |
1 | 5 | 4 |
1 | 11 | 10 |
5 | 11 | 14 |
Approach used in the below program is as follows
Input an array of integer elements to form an pair
Calculate the size of an array pass the data to the function for further processing
Create a temporary variable count to store the pairs formed with XOR operation as an even value.
Start loop FOR from i to 0 till the size of an array
Inside the loop, check IF arr[i] % 2 == FALSE then increment the count by 1
Set the temp as size * (size - 1)
Set another temporary variable as pairs = temp / 2
Now calculate the odd value pairs in an array as count * (size - count)
Now calculate the even pairs as total pairs - odd pairs
Return the even
Print the result.
Example
#include <iostream> using namespace std; //Count pairs with Bitwise XOR as EVEN number int XOR_Even(int arr[], int size){ int count = 0; for (int i = 0; i < size; i++){ if (arr[i] % 2 != 0){ count++; } } int temp = size * (size-1); int Pairs = temp / 2; int odd = count * (size - count); int even = Pairs - odd; return even; } int main(){ int arr[] = { 2, 6, 1, 8}; int size = sizeof(arr) / sizeof(arr[0]); cout<<"Count of pairs with Bitwise XOR as EVEN number are: "<<XOR_Even(arr, size); return 0; }
Output
If we run the above code it will generate the following output −
Count of pairs with Bitwise XOR as EVEN number are: 3
- Related Articles
- Count pairs with Bitwise XOR as ODD number in C++
- Count pairs with Bitwise-AND as even number in C++
- Count pairs with Bitwise OR as Even number in C++
- Count pairs with Bitwise AND as ODD number in C++
- Number of pairs with Bitwise OR as Odd number in C++
- Count pairs with Odd XOR in C++
- Count all pairs with given XOR in C++
- Count number of ordered pairs with Even and Odd Product in C++
- Count number of ordered pairs with Even and Odd Sums in C++
- Count pairs with bitwise OR less than Max in C++
- Program to count pairs with XOR in a range in Python
- Program to find XOR sum of all pairs bitwise AND in Python
- Count Pairs from two arrays with even sum in C++
- Bitwise XOR in Arduino
- Count all pairs of adjacent nodes whose XOR is an odd number in C++
