# Number of pairs with Bitwise OR as Odd 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 odd.

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 Odd = 3

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

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

A Simple Solution is to check every pair and find the Bitwise-OR and count all such pairs with Bitwise OR as odd.

Below is the implementation of the above approach:

## C++

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

## Java

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

## Python3

 `     `  `# Python3 program to count pairs with odd OR ` ` `  `  `  `# Function to count pairs with odd OR ` `def` `findOddPair(A, N): ` `     `  `    ``oddPair ``=` `0` `    ``for` `i ``in` `range``(``0``, N): ` `        ``for` `j ``in` `range``(i``+``1``, N): ` `  `  `            ``# find OR operation ` `            ``# check odd or odd ` `            ``if` `((A[i] | A[j]) ``%` `2` `!``=` `0``): ` `                ``oddPair``+``=``1` ` `  `    ``# return count of odd pair ` `    ``return` `oddPair ` ` `  `  `  `# Driver Code ` `def` `main(): ` `     `  `    ``A ``=` `[ ``5``, ``6``, ``2``, ``8` `] ` `  `  `    ``N ``=` `len``(A) ` `  `  `    ``print``(findOddPair(A, N)) ` ` `  `if` `__name__ ``=``=` `'__main__'``: ` `    ``main() ` `# This code is contributed by PrinciRaj1992   `

## C#

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

## PHP

 ` `

Output:

```3
```

Time Complexity: O(N2)

An Efficient Solution is to count pairs with even OR and subtract them with total number of pairs to get pairs with odd Bitwise-OR. To do this, count numbers with last bit as 0. Then number of pairs with even Bitwise-OR = count * (count – 1)/2 and total number of pairs will be N*(N-1)/2.

Therefore, pairs with ODD Bitwise-OR will be:

```Total Pairs - Pairs with EVEN Bitwise-OR
```

Below is the implementation of the above approach:

## C++

 `// C++ program to count pairs with odd OR ` `#include ` `using` `namespace` `std; ` ` `  `// Function to count pairs with odd OR ` `int` `countOddPair(``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++; ` ` `  `    ``// Even pair count ` `    ``int` `evenPairCount = count * (count - 1) / 2; ` ` `  `    ``// Total pairs ` `    ``int` `totPairs = N * (N - 1) / 2; ` ` `  `    ``// Return Odd pair count ` `    ``return` `totPairs - evenPairCount; ` `} ` ` `  `// Driver main ` `int` `main() ` `{ ` `    ``int` `A[] = { 5, 6, 2, 8 }; ` `    ``int` `N = ``sizeof``(A) / ``sizeof``(A); ` ` `  `    ``cout << countOddPair(A, N) << endl; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to count pairs with odd OR ` ` `  `public` `class` `GFG { ` ` `  `// Function to count pairs with odd OR ` `    ``static` `int` `countOddPair(``int` `A[], ``int` `N) { ` `        ``// Count total even numbers in ` `        ``// array ` ` `  `        ``int` `count = ``0``; ` `        ``for` `(``int` `i = ``0``; i < N; i++) { ` `            ``if` `((A[i] % ``2` `!= ``1``)) { ` `                ``count++; ` `            ``} ` `        ``} ` ` `  `        ``// Even pair count ` `        ``int` `evenPairCount = count * (count - ``1``) / ``2``; ` ` `  `        ``// Total pairs ` `        ``int` `totPairs = N * (N - ``1``) / ``2``; ` ` `  `        ``// Return Odd pair count ` `        ``return` `totPairs - evenPairCount; ` `    ``} ` ` `  `// Driver main ` `    ``public` `static` `void` `main(String[] args) { ` `        ``int` `A[] = {``5``, ``6``, ``2``, ``8``}; ` `        ``int` `N = A.length; ` ` `  `        ``System.out.println(countOddPair(A, N)); ` ` `  `    ``} ` `} `

## Python3

 `# Python 3program to count pairs with odd OR ` ` `  `# Function to count pairs with odd OR ` `def` `countOddPair(A, N): ` `     `  `    ``# Count total even numbers in ` `    ``# array ` `    ``count ``=` `0` `    ``for` `i ``in` `range``(``0``, N): ` `        ``if` `(A[i] ``%` `2` `!``=` `1``): ` `            ``count``+``=``1` ` `  `    ``# Even pair count ` `    ``evenPairCount ``=` `count ``*` `(count ``-` `1``) ``/` `2` ` `  `    ``# Total pairs ` `    ``totPairs ``=` `N ``*` `(N ``-` `1``) ``/` `2` ` `  `    ``# Return Odd pair count ` `    ``return` `(``int``)(totPairs ``-` `evenPairCount) ` `     `  `# Driver Code ` `A ``=` `[ ``5``, ``6``, ``2``, ``8` `] ` ` `  `N ``=` `len``(A) ` ` `  `print``(countOddPair(A, N)) ` ` `  `# This code is contributed by PrinciRaj1992  `

## C#

 `// C# program to count pairs with odd OR ` `using` `System;  ` ` `  `public` `class` `GFG {  ` ` `  `// Function to count pairs with odd OR ` `    ``static` `int` `countOddPair(``int` `[]A, ``int` `N) { ` `        ``// Count total even numbers in ` `        ``// array ` `  `  `        ``int` `count = 0; ` `        ``for` `(``int` `i = 0; i < N; i++) { ` `            ``if` `((A[i] % 2 != 1)) { ` `                ``count++; ` `            ``} ` `        ``} ` `  `  `        ``// Even pair count ` `        ``int` `evenPairCount = count * (count - 1) / 2; ` `  `  `        ``// Total pairs ` `        ``int` `totPairs = N * (N - 1) / 2; ` `  `  `        ``// Return Odd pair count ` `        ``return` `totPairs - evenPairCount; ` `    ``} ` `  `  `// Driver main ` `    ``public` `static` `void` `Main() { ` `        ``int` `[]A = {5, 6, 2, 8}; ` `        ``int` `N = A.Length; ` `  `  `        ``Console.WriteLine(countOddPair(A, N)); ` `  `  `    ``} ` `} ` `/*This code is contributed by PrinciRaj1992*/`

## PHP

 ` `

Output:

```3
```

Time Complexity: O(N)

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.