# Count pairs with Bitwise OR as Even number

Given an array A[] of size N. The task is to find the how many pair(i, j) exists such that A[i] OR A[j] is even.

Examples:

```Input : N = 4
A[] = { 5, 6, 2, 8 }
Output :3
Explanation :
Since pair of A[] = ( 5, 6 ), ( 5, 2 ), ( 5, 8 ),
( 6, 2 ), ( 6, 8 ), ( 2, 8 )
5 OR 6 = 7, 5 OR 2 = 7, 5 OR 8 = 13
6 OR 2 = 6, 6 OR 8 = 14, 2 OR 8 = 10
Total pair A( i, j ) = 6 and Even = 3

Input : N = 7
A[] = {8, 6, 2, 7, 3, 4, 9}
Output :6
```

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

A simple solution is to check every pair.

## C++

 `// C++ program to count pairs with even OR ` `#include ` `using` `namespace` `std; ` ` `  `int` `findEvenPair(``int` `A[], ``int` `N) ` `{ ` `    ``int` `evenPair = 0; ` `    ``for` `(``int` `i = 0; i < N; i++) { ` `        ``for` `(``int` `j = i + 1; j < N; j++) { ` ` `  `            ``// find OR operation ` `            ``// check odd or even ` `            ``if` `((A[i] | A[j]) % 2 == 0) ` `                ``evenPair++; ` `        ``} ` `    ``} ` `    ``// return count of even pair ` `    ``return` `evenPair; ` `} ` `// Driver main ` `int` `main() ` `{ ` `    ``int` `A[] = { 5, 6, 2, 8 }; ` `    ``int` `N = ``sizeof``(A) / ``sizeof``(A); ` `    ``cout << findEvenPair(A, N) << endl; ` `    ``return` `0; ` `} `

## Java

 `// Java program to count ` `// pairs with even OR ` `import` `java.io.*; ` ` `  `class` `GFG  ` `{ ` ` `  `static` `int` `findEvenPair(``int` `A[],  ` `                        ``int` `N) ` `{ ` `    ``int` `evenPair = ``0``; ` `    ``for` `(``int` `i = ``0``; i < N; i++)  ` `    ``{ ` `        ``for` `(``int` `j = i + ``1``; j < N; j++) ` `        ``{ ` ` `  `            ``// find OR operation ` `            ``// check odd or even ` `            ``if` `((A[i] | A[j]) % ``2` `== ``0``) ` `                ``evenPair++; ` `        ``} ` `    ``} ` `     `  `    ``// return count of even pair ` `    ``return` `evenPair; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main (String[] args) ` `{ ` `    ``int` `A[] = { ``5``, ``6``, ``2``, ``8` `}; ` `    ``int` `N = A.length; ` `    ``System.out.println(findEvenPair(A, N)); ` `} ` `} ` ` `  `// This code is contributed ` `// by inder_verma. `

## Python 3

 `# Python 3 program to count pairs ` `# with even OR ` ` `  `def` `findEvenPair(A, N) : ` `    ``evenPair ``=` `0` ` `  `    ``for` `i ``in` `range``(N) : ` `        ``for` `j ``in` `range``(i``+``1``, N): ` ` `  `            ``# find OR operation  ` `            ``# check odd or even  ` `            ``if` `(A[i] | A[j]) ``%` `2` `=``=` `0` `: ` `                ``evenPair ``+``=` `1` ` `  `    ``# return count of even pair ` `    ``return` `evenPair ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `"__main__"` `: ` ` `  `    ``A ``=` `[ ``5``, ``6``, ``2``, ``8``] ` `    ``N ``=` `len``(A) ` ` `  `    ``# function calling ` `    ``print``(findEvenPair(A, N)) ` ` `  `# This code is contributed by ANKITRAI1 `

## C#

 `// C# program to count ` `// pairs with even OR ` `using` `System; ` ` `  `class` `GFG  ` `{ ` ` `  `static` `int` `findEvenPair(``int` `[]A,  ` `                        ``int` `N) ` `{ ` `    ``int` `evenPair = 0; ` `    ``for` `(``int` `i = 0; i < N; i++)  ` `    ``{ ` `        ``for` `(``int` `j = i + 1; j < N; j++) ` `        ``{ ` ` `  `            ``// find OR operation ` `            ``// check odd or even ` `            ``if` `((A[i] | A[j]) % 2 == 0) ` `                ``evenPair++; ` `        ``} ` `    ``} ` `     `  `    ``// return count of even pair ` `    ``return` `evenPair; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main () ` `{ ` `    ``int` `[]A = { 5, 6, 2, 8 }; ` `    ``int` `N = A.Length; ` `    ``Console.WriteLine(findEvenPair(A, N)); ` `} ` `} ` ` `  `// This code is contributed ` `// by inder_verma. `

## PHP

 ` `

Output:

```3
```

Time Complexity: O(N^2)

A efficient solution is to count numbers with last bit as 0. Then return count * (count – 1)/2. Note that OR of two numbers can be even only if their last bits are 0.

## C++

 `// C++ program to count pairs with even OR ` `#include ` `using` `namespace` `std; ` ` `  `int` `findEvenPair(``int` `A[], ``int` `N) ` `{  ` `    ``// Count total even numbers in ` `    ``// array. ` `    ``int` `count = 0; ` `    ``for` `(``int` `i = 0; i < N; i++)  ` `       ``if` `(!(A[i] & 1)) ` `            ``count++; ` `             `  `    ``// return count of even pair ` `    ``return` `count * (count - 1) / 2; ` `} ` ` `  `// Driver main ` `int` `main() ` `{ ` `    ``int` `A[] = { 5, 6, 2, 8 }; ` `    ``int` `N = ``sizeof``(A) / ``sizeof``(A); ` `    ``cout << findEvenPair(A, N) << endl; ` `    ``return` `0; ` `} `

## Java

 `// Java program to count ` `// pairs with even OR ` `import` `java.io.*; ` ` `  `class` `GFG  ` `{ ` `static` `int` `findEvenPair(``int` `A[], ``int` `N) ` `{  ` `    ``// Count total even numbers in ` `    ``// array. ` `    ``int` `count = ``0``; ` `    ``for` `(``int` `i = ``0``; i < N; i++)  ` `    ``if` `((!((A[i] & ``1``) > ``0``))) ` `            ``count++; ` `             `  `    ``// return count of even pair ` `    ``return` `count * (count - ``1``) / ``2``; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main (String[] args)  ` `{ ` `    ``int` `A[] = { ``5``, ``6``, ``2``, ``8` `}; ` `    ``int` `N = A.length; ` `    ``System.out.println(findEvenPair(A, N)); ` `} ` `} ` ` `  `// This code is contributed ` `// by inder_verma. `

## Python 3

 `# Python 3 program to count  ` `# pairs with even OR ` ` `  `def` `findEvenPair(A, N): ` ` `  `    ``# Count total even numbers  ` `    ``# in array. ` `    ``count ``=` `0` `    ``for` `i ``in` `range``(N):  ` `        ``if` `(``not` `(A[i] & ``1``)): ` `            ``count ``+``=` `1` `           `  `    ``# return count of even pair ` `    ``return` `count ``*` `(count ``-` `1``) ``/``/` `2` ` `  `# Driver Code ` `if` `__name__ ``=``=` `"__main__"``: ` `    ``A ``=` `[ ``5``, ``6``, ``2``, ``8` `] ` `    ``N ``=` `len``(A) ` `    ``print``(findEvenPair(A, N)) ` ` `  `# This code is contributed ` `# by ChitraNayal `

## C#

 `// C# program to count ` `// pairs with even OR ` `using` `System; ` ` `  `class` `GFG  ` `{ ` `static` `int` `findEvenPair(``int` `[]A, ``int` `N) ` `{  ` `    ``// Count total even numbers  ` `    ``// in array. ` `    ``int` `count = 0; ` `    ``for` `(``int` `i = 0; i < N; i++)  ` `    ``if` `((!((A[i] & 1) > 0))) ` `            ``count++; ` `             `  `    ``// return count of even pair ` `    ``return` `count * (count - 1) / 2; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main (String[] args)  ` `{ ` `    ``int` `[]A = { 5, 6, 2, 8 }; ` `    ``int` `N = A.Length; ` `    ``Console.WriteLine(findEvenPair(A, N)); ` `} ` `} ` ` `  `// This code is contributed  ` `// by Kirti_Mangal `

## PHP

 ` `

Output:

```3
```

Time Complexity: O(N)

My Personal Notes arrow_drop_up Strategy Path planning and Destination matters in success No need to worry about in between temporary failures

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 :

2

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