# Count pairs with bitwise OR less than Max

Given an array arr[] of N integers, the task is to count the pairs of indices (i, j) such that 0 ≤ i < j ≤ N and arr[i] | arr[j] ≤ max(arr[i], arr[j]) where | is the bitwise OR.

Examples:

Input: arr[] = {1, 2, 3}
Output: 2
(1, 3) and (2, 3) are the only valid pairs.

Input: arr[] = {11, 45, 12, 14, 5}
Output: 3

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Run two nested loops and check for every possible pair. If arr[i] | arr[j] <= max(arr[i], arr[j]) then increment the count.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the count of valid pairs ` `int` `countPairs(``int` `arr[], ``int` `n) ` `{ ` `    ``int` `cnt = 0; ` ` `  `    ``// Check all possible pairs ` `    ``for` `(``int` `i = 0; i < n - 1; i++) ` `        ``for` `(``int` `j = i + 1; j < n; j++) ` `            ``if` `((arr[i] | arr[j]) <= max(arr[i], arr[j])) ` `                ``cnt++; ` ` `  `    ``return` `cnt; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 1, 2, 3 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``cout << countPairs(arr, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` ` `  `class` `GFG ` `{ ` `     `  `// Function to return the count of valid pairs ` `static` `int` `countPairs(``int` `arr[], ``int` `n) ` `{ ` `    ``int` `cnt = ``0``; ` ` `  `    ``// Check all possible pairs ` `    ``for` `(``int` `i = ``0``; i < n - ``1``; i++) ` `        ``for` `(``int` `j = i + ``1``; j < n; j++) ` `            ``if` `((arr[i] | arr[j]) <= Math.max(arr[i], arr[j])) ` `                ``cnt++; ` ` `  `    ``return` `cnt; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `arr[] = { ``1``, ``2``, ``3` `}; ` `    ``int` `n = arr.length; ` `    ``System.out.println(countPairs(arr, n)); ` `} ` `} ` ` `  `// This code is Contributed by Code_Mech. `

## Python3

 `# Python 3 implementation of the approach ` ` `  `# Function to return the count  ` `# of valid pairs ` `def` `countPairs(arr, n): ` `    ``cnt ``=` `0` ` `  `    ``# Check all possible pairs ` `    ``for` `i ``in` `range``(n ``-` `1``): ` `        ``for` `j ``in` `range``(i ``+` `1``, n, ``1``): ` `            ``if` `((arr[i] | arr[j]) <``=` `max``(arr[i],  ` `                                         ``arr[j])): ` `                ``cnt ``+``=` `1` ` `  `    ``return` `cnt ` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``arr ``=` `[``1``, ``2``, ``3``] ` `    ``n ``=` `len``(arr) ` `    ``print``(countPairs(arr, n)) ` `     `  `# This code is contributed by ` `# Surendra_Gangwar `

## C#

 `// C# implementation of the approach ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `// Function to return the count of valid pairs ` `static` `int` `countPairs(``int` `[]arr, ``int` `n) ` `{ ` `    ``int` `cnt = 0; ` ` `  `    ``// Check all possible pairs ` `    ``for` `(``int` `i = 0; i < n - 1; i++) ` `        ``for` `(``int` `j = i + 1; j < n; j++) ` `            ``if` `((arr[i] | arr[j]) <= Math.Max(arr[i], arr[j])) ` `                ``cnt++; ` ` `  `    ``return` `cnt; ` `} ` ` `  `// Driver code ` `static` `void` `Main() ` `{ ` `    ``int` `[]arr = { 1, 2, 3 }; ` `    ``int` `n = arr.Length; ` `    ``Console.WriteLine(countPairs(arr, n)); ` `} ` `} ` ` `  `// This code is Contributed by mits. `

## PHP

 ` `

Output:

```2
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.