Count pairs with Bitwise XOR as ODD number
Last Updated :
02 Sep, 2022
Given an array of N integers, the task is to find the number of pairs (i, j) such that A[i] ^ A[j] is odd.
Examples:
Input : N = 5
A[] = { 5, 4, 7, 2, 1}
Output :6
Since pair of A[] =
( 5, 4 ) = 1( 5, 7 ) = 2( 5, 2 ) = 7( 5, 1 ) = 4
( 4, 7 ) = 3( 4, 2 ) = 6( 4, 1 ) = 5
( 7, 2 ) = 5( 7, 1 ) = 6
( 2, 1 ) = 3
Total XOR ODD pair = 6
Input : N = 7
A[] = { 7, 2, 8, 1, 0, 5, 11 }
Output :12
Since pair of A[] =
( 7, 2 ) = 5( 7, 8 ) = 15( 7, 1 ) = 6( 7, 0 ) = 7( 7, 5 ) = 2( 7, 11 ) = 12
( 2, 8 ) = 10( 2, 1 ) = 3( 2, 0 ) = 2( 2, 5 ) = 7( 2, 11 ) = 9
( 8, 1 ) = 9( 8, 0 ) = 8( 8, 5 ) = 13( 8, 11 ) = 3
( 1, 0 ) = 1( 1, 5 ) = 4( 1, 11 ) = 10
( 0, 5 ) = 5( 0, 11 ) = 11
( 5, 11 ) = 14
A naive approach is to check for every pair and print the count of pairs.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int findOddPair( int A[], int N)
{
int i, j;
int oddPair = 0;
for (i = 0; i < N; i++) {
for (j = i + 1; j < N; j++) {
if ((A[i] ^ A[j]) % 2 != 0)
oddPair++;
}
cout << endl;
}
return oddPair;
}
int main()
{
int A[] = { 5, 4, 7, 2, 1 };
int N = sizeof (A) / sizeof (A[0]);
cout << findOddPair(A, N) << endl;
return 0;
}
|
Java
class GFG
{
static int findOddPair( int A[],
int N)
{
int i, j;
int 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;
}
public static void main(String args[])
{
int A[] = { 5 , 4 , 7 , 2 , 1 };
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
if __name__ = = '__main__' :
A = [ 5 , 4 , 7 , 2 , 1 ]
N = len (A)
print (findOddPair(A, N))
|
C#
using System;
class GFG
{
static int findOddPair( int [] A,
int N)
{
int i, j;
int 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;
}
public static void Main()
{
int [] A = { 5, 4, 7, 2, 1 };
int N = A.Length;
Console.WriteLine(findOddPair(A, N));
}
}
# calling function findOddPair
# and print number of odd pair
print(findOddPair(a, n))
|
PHP
<?php
function findOddPair( $A , $N )
{
$i ; $j ;
$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, 4, 7, 2, 1 );
$N = sizeof( $A ) / sizeof( $A [0]);
echo findOddPair( $A , $N ), "\n" ;
?>
|
Javascript
<script>
function findOddPair(A, N)
{
let i, j;
let 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;
}
let A = [ 5, 4, 7, 2, 1 ];
let N = A.length;
document.write(findOddPair(A, N) + "<br>" );
</script>
|
OUTPUT:
6
Time Complexity: O(N^2) as two nested loops are being used.
Auxiliary Space: O(1), as constant space is being used by the algorithm.
An efficient solution is to count the even numbers. Then return count * (N – count).
C++
#include <iostream>
using namespace std;
int findOddPair( int A[], int N)
{
int i, count = 0;
for (i = 0; i < N; i++) {
if (A[i] % 2 == 0)
count++;
}
return count * (N - count);
}
int main()
{
int a[] = { 5, 4, 7, 2, 1 };
int n = sizeof (a) / sizeof (a[0]);
cout << findOddPair(a, n) << endl;
return 0;
}
|
Java
class GFG
{
static int findOddPair( int A[],
int N)
{
int i, count = 0 ;
for (i = 0 ; i < N; i++)
{
if (A[i] % 2 == 0 )
count++;
}
return count * (N - count);
}
public static void main(String[] arg)
{
int a[] = { 5 , 4 , 7 , 2 , 1 };
int n = a.length ;
System.out.println(findOddPair(a, n));
}
}
|
Python3
def findOddPair(A, N) :
count = 0
for i in range ( 0 , N) :
if (A[i] % 2 = = 0 ) :
count + = 1
return count * (N - count)
if __name__ = = '__main__' :
a = [ 5 , 4 , 7 , 2 , 1 ]
n = len (a)
print (findOddPair(a,n))
|
C#
using System;
class GFG
{
static int findOddPair( int []A,
int N)
{
int i, count = 0;
for (i = 0; i < N; i++)
{
if (A[i] % 2 == 0)
count++;
}
return count * (N - count);
}
public static void Main()
{
int []a = { 5, 4, 7, 2, 1 };
int n = a.Length ;
Console.Write(findOddPair(a, n));
}
}
|
PHP
<?php
function findOddPair( $A , $N )
{
$count = 0;
for ( $i = 0; $i < $N ; $i ++)
{
if ( $A [ $i ] % 2 == 0)
$count ++;
}
return $count * ( $N - $count );
}
$a = array ( 5, 4, 7, 2, 1 );
$n = count ( $a );
echo ( findOddPair( $a , $n ));
?>
|
Javascript
<script>
function findOddPair(A, N)
{
let i, count = 0;
for (i = 0; i < N; i++) {
if (A[i] % 2 == 0)
count++;
}
return count * (N - count);
}
let a = [ 5, 4, 7, 2, 1 ];
let n = a.length;
document.write(findOddPair(a, n));
</script>
|
Output:
6
Time Complexity: O(N), since one traversal of the array is required to complete all operations.
Auxiliary Space: O(1), as constant space is being used.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...