- 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 less than Max 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 the value less than MAX value in the given pair.

**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 less than Max are − 3

**Explanation** −

X | Y | X V Y |

2 | 5 | 7>5=FALSE |

2 | 1 | 3>2=FALSE |

2 | 8 | 10>8=FALSE |

2 | 9 | 11>9=FALSE |

5 | 1 | 5=5 TRUE |

5 | 8 | 13>8=FALSE |

5 | 9 | 13>9=FALSE |

1 | 8 | 9>8=FALSE |

1 | 9 | 10>9=FALSE |

8 | 9 | 9=9= TRUE |

## 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 having value less than or equals to MAX value amongst the pair

Start loop FOR from i to 0 till the size-1 of an array

Inside the loop, start another loop FOR from j to i+1 till the size of an array

Inside the loop, check IF arr[i] | arr[j] <= max(arr[i], arr[j]) then increment the count by 1

Return the count

Print the result.

## Example

#include <bits/stdc++.h> using namespace std; //Count pairs with bitwise OR less than Max int Pair_OR(int arr[], int size){ int count = 0; for (int i = 0; i lt; size - 1; i++){ for (int j = i + 1; j lt; size; j++){ if ((arr[i] | arr[j]) lt;= max(arr[i], arr[j])){ count++; } } } return count; } int main(){ int arr[] = { 4, 8, 9, 10, 23}; int size = sizeof(arr) / sizeof(arr[0]); cout<<"Count of pairs with bitwise OR less than Max are: "<<Pair_OR(arr, size); return 0; }

## Output

If we run the above code it will generate the following output −

Count of pairs with bitwise OR less than Max are − 3

- Related Questions & Answers
- Count ordered pairs with product less than N in C++
- Count pairs with Bitwise OR as Even number in C++
- Count pairs with sum as a prime number and less than n in C++
- Count pairs with Bitwise AND as ODD number in C++
- Count pairs with Bitwise-AND as even number in C++
- Count pairs with Bitwise XOR as EVEN number in C++
- Count pairs with Bitwise XOR as ODD number in C++
- Count pairs in a sorted array whose sum is less than x in C++
- Count pairs in a sorted array whose product is less than k in C++
- Python – Elements with factors count less than K
- Number of pairs with Bitwise OR as Odd number in C++
- Find unique pairs such that each element is less than or equal to N in C++
- Count sub-arrays which have elements less than or equal to X in C++
- C++ program to find unique pairs such that each element is less than or equal to N
- Count all subsequences having product less than K in C++