Number of pairs with Bitwise OR as Odd number
Last Updated :
19 Sep, 2022
Given an array A[] of size N. The task is to find 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
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++
#include <iostream>
using namespace std;
int findOddPair( int A[], int N)
{
int oddPair = 0;
for ( int i = 0; i < N; i++) {
for ( int j = i + 1; j < N; j++) {
if ((A[i] | A[j]) % 2 != 0)
oddPair++;
}
}
return oddPair;
}
int main()
{
int A[] = { 5, 6, 2, 8 };
int N = sizeof (A) / sizeof (A[0]);
cout << findOddPair(A, N) << endl;
return 0;
}
|
C
#include <stdio.h>
int findOddPair( int A[], int N)
{
int oddPair = 0;
for ( int i = 0; i < N; i++) {
for ( int j = i + 1; j < N; j++) {
if ((A[i] | A[j]) % 2 != 0)
oddPair++;
}
}
return oddPair;
}
int main()
{
int A[] = { 5, 6, 2, 8 };
int N = sizeof (A) / sizeof (A[0]);
printf ( "%d\n" ,findOddPair(A, N));
return 0;
}
|
Java
class GFG
{
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++)
{
if ((A[i] | A[j]) % 2 != 0 )
oddPair++;
}
}
return oddPair;
}
public static void main(String []args)
{
int A[] = { 5 , 6 , 2 , 8 };
int N = A.length;
System.out.println(findOddPair(A, N));
}
}
|
Python3
def findOddPair(A, N):
oddPair = 0
for i in range ( 0 , N):
for j in range (i + 1 , N):
if ((A[i] | A[j]) % 2 ! = 0 ):
oddPair + = 1
return oddPair
def main():
A = [ 5 , 6 , 2 , 8 ]
N = len (A)
print (findOddPair(A, N))
if __name__ = = '__main__' :
main()
|
C#
using System;
public class GFG{
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++)
{
if ((A[i] | A[j]) % 2 != 0)
oddPair++;
}
}
return oddPair;
}
static public void Main (){
int []A = { 5, 6, 2, 8 };
int N = A.Length;
Console.WriteLine(findOddPair(A, N));
}
}
|
PHP
<?php
function findOddPair( $A , $N )
{
$oddPair = 0;
for ( $i = 0; $i < $N ; $i ++) {
for ( $j = $i + 1; $j < $N ; $j ++) {
if (( $A [ $i ] | $A [ $j ]) % 2 != 0)
$oddPair ++;
}
}
return $oddPair ;
}
$A = array (5, 6, 2, 8 );
$N = sizeof( $A ) / sizeof( $A [0]);
echo findOddPair( $A , $N ), "\n" ;
#This code is contributed by ajit
?>
|
Javascript
<script>
function findOddPair(A, N)
{
let oddPair = 0;
for (let i = 0; i < N; i++)
{
for (let j = i + 1; j < N; j++)
{
if ((A[i] | A[j]) % 2 != 0)
oddPair++;
}
}
return oddPair;
}
let A = [ 5, 6, 2, 8 ];
let N = A.length;
document.write(findOddPair(A, N));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
An Efficient Solution is to count pairs with even OR and subtract them with a total number of pairs to get pairs with odd Bitwise-OR. To do this, count numbers with last bit as 0. Then a 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++
#include <iostream>
using namespace std;
int countOddPair( int A[], int N)
{
int count = 0;
for ( int i = 0; i < N; i++)
if (!(A[i] & 1))
count++;
int evenPairCount = count * (count - 1) / 2;
int totPairs = N * (N - 1) / 2;
return totPairs - evenPairCount;
}
int main()
{
int A[] = { 5, 6, 2, 8 };
int N = sizeof (A) / sizeof (A[0]);
cout << countOddPair(A, N) << endl;
return 0;
}
|
Java
public class GFG {
static int countOddPair( int A[], int N) {
int count = 0 ;
for ( int i = 0 ; i < N; i++) {
if ((A[i] % 2 != 1 )) {
count++;
}
}
int evenPairCount = count * (count - 1 ) / 2 ;
int totPairs = N * (N - 1 ) / 2 ;
return totPairs - evenPairCount;
}
public static void main(String[] args) {
int A[] = { 5 , 6 , 2 , 8 };
int N = A.length;
System.out.println(countOddPair(A, N));
}
}
|
Python3
def countOddPair(A, N):
count = 0
for i in range ( 0 , N):
if (A[i] % 2 ! = 1 ):
count + = 1
evenPairCount = count * (count - 1 ) / 2
totPairs = N * (N - 1 ) / 2
return ( int )(totPairs - evenPairCount)
A = [ 5 , 6 , 2 , 8 ]
N = len (A)
print (countOddPair(A, N))
|
C#
using System;
public class GFG {
static int countOddPair( int []A, int N) {
int count = 0;
for ( int i = 0; i < N; i++) {
if ((A[i] % 2 != 1)) {
count++;
}
}
int evenPairCount = count * (count - 1) / 2;
int totPairs = N * (N - 1) / 2;
return totPairs - evenPairCount;
}
public static void Main() {
int []A = {5, 6, 2, 8};
int N = A.Length;
Console.WriteLine(countOddPair(A, N));
}
}
|
PHP
<?php
function countOddPair( $A , $N )
{
$count = 0;
for ( $i = 0; $i < $N ; $i ++)
if (!( $A [ $i ] & 1))
$count ++;
$evenPairCount = $count *
( $count - 1) / 2;
$totPairs = $N * ( $N - 1) / 2;
return ( $totPairs - $evenPairCount );
}
$A = array ( 5, 6, 2, 8 );
$N = sizeof( $A );
echo countOddPair( $A , $N ), "\n" ;
?>
|
Javascript
<script>
function countOddPair(A, N)
{
let count = 0;
for (let i = 0; i < N; i++)
if (!(A[i] & 1))
count++;
let evenPairCount =
parseInt(count * (count - 1) / 2);
let totPairs = parseInt(N * (N - 1) / 2);
return totPairs - evenPairCount;
}
let A = [ 5, 6, 2, 8 ];
let N = A.length;
document.write(countOddPair(A, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...