
- 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
Number of pairs with Bitwise OR as Odd number in C++
Given an array, we have to find the number of pairs whose Bitwise OR is an odd number. Let's see the example.
Input
arr = [1, 2]
Output
1
There is only one pair whose Bitwise OR is an odd number. And the pair is (1, 2).
Algorithm
- Initialise the array with random numbers.
- Initialise the count to 0.
- Write two loops to get the pairs of the array.
- Compute the bitwise OR between every pair.
- Increment the count if the result is an odd number.
- Return the count.
Implementation
Following is the implementation of the above algorithm in C++
#include <bits/stdc++.h> using namespace std; int getOddPairsCount(int arr[], int n) { int count = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if ((arr[i] | arr[j]) % 2 != 0) { count++; } } } return count; } int main() { int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int n = 10; cout << getOddPairsCount(arr, n) << endl; return 0; }
Output
If you run the above code, then you will get the following result.
35
- Related Articles
- Count pairs with Bitwise AND as ODD number in C++
- Count pairs with Bitwise XOR as ODD number in C++
- Count pairs with Bitwise OR as Even number in C++
- Count pairs with Bitwise-AND 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 bitwise OR less than Max in C++
- Number of integers with odd number of set bits in C++
- Number of pairs with maximum sum in C++
- Multiply any Number with using Bitwise Operator in C++
- Count all pairs of adjacent nodes whose XOR is an odd number in C++
- Find the number of subarrays have bitwise OR >= K using C++
- Express an odd number as sum of prime numbers in C++
- Count pairs with Odd XOR in C++

Advertisements