
- 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 AND as ODD 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 AND operation on the pairs will result in an odd number.
The truth table for AND operation is given below
A | B | A^B |
0 | 0 | 0 |
1 | 0 | 0 |
0 | 1 | 0 |
1 | 1 | 1 |
Input − int arr[] = {2, 5, 1, 8, 9}
Output − Count of pairs with Bitwise AND as ODD number are − 3
Explanation −
a1 | a2 | a1^a2 |
2 | 5 | 0 |
2 | 1 | 0 |
2 | 8 | 0 |
2 | 9 | 0 |
5 | 1 | 1 |
5 | 8 | 0 |
5 | 9 | 1 |
1 | 8 | 0 |
1 | 9 | 1 |
8 | 9 | 8 |
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 AND operation as an odd value.
Start loop FOR from i to 0 till the size of an array
Inside the loop, check IF arr[i] % 2 == TRUE then increment the count by 1
Set the count as count * (count - 1) / 2
Return the count
Print the result.
Example
#include <iostream> using namespace std; //Count pairs with Bitwise AND as ODD number int count_pair(int arr[], int size){ int count = 0; for (int i = 0; i < size; i++){ if ((arr[i] % 2 == 1)){ count++; } } count = count * (count - 1) / 2; return count; } int main(){ int arr[] = {2, 5, 1, 8, 9, 2, 7}; int size = sizeof(arr) / sizeof(arr[0]); cout<<"Count of pairs with Bitwise AND as ODD number are: "<<count_pair(arr, size) << endl; return 0; }
Output
If we run the above code it will generate the following output −
Count of pairs with Bitwise AND as ODD number are: 6
- Related Articles
- Count pairs with Bitwise XOR as ODD number in C++
- Count pairs with Bitwise-AND as even number in C++
- Number of pairs with Bitwise OR as Odd number in C++
- Count pairs with Bitwise OR as Even number in C++
- Count pairs with Bitwise XOR as EVEN number 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 Odd XOR in C++
- Count pairs with bitwise OR less than Max in C++
- Count pairs with sum as a prime number and less than n in C++
- Count all pairs of adjacent nodes whose XOR is an odd number in C++
- Count number of pairs (A
- Count odd and even digits in a number in PL/SQL
- Program to count number of valid pairs from a list of numbers, where pair sum is odd in Python
- Count subarrays with same even and odd elements in C++
