- 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 pairs with Bitwise OR 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 OR operation on the pairs will result in an even number.

**The truth table for OR operation is given below**

A | B | AVB |

0 | 0 | 0 |

1 | 0 | 1 |

0 | 1 | 1 |

1 | 1 | 1 |

**Input** − int arr[] = {2, 5, 1, 8, 9}

**Output** − Count of pairs with Bitwise OR as EVEN number are − 2

**Explanation** −

a1 | a2 | a1Va2 |

2 | 5 | 7 |

2 | 1 | 3 |

2 | 8 | 10 |

2 | 9 | 11 |

5 | 1 | 5 |

5 | 8 | 13 |

5 | 9 | 13 |

1 | 8 | 9 |

1 | 9 | 10 |

8 | 9 | 9 |

## 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 OR 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] & 1 == FALSE 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] & 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 OR as Even 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 OR as Even number are: 3

- Related Questions & Answers
- Count pairs with Bitwise-AND as even number in C++
- Count pairs with Bitwise XOR as EVEN number in C++
- Count pairs with Bitwise AND as ODD number in C++
- Count pairs with Bitwise XOR as ODD number in C++
- Number of pairs with Bitwise OR as Odd number in C++
- Count pairs with bitwise OR less than Max 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 from two arrays with even sum in C++
- Count pairs with sum as a prime number and less than n in C++
- Find subsequences with maximum Bitwise AND and Bitwise OR in Python
- Count pairs with given sum in C++
- Count pairs with Odd XOR in C++
- Determining sum of array as even or odd in JavaScript
- Even Number With Prime Sum