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
Implementation: A simple solution is to check every pair.
C++
#include <iostream>
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++) {
if ((A[i] | A[j]) % 2 == 0)
evenPair++;
}
}
return evenPair;
}
int main()
{
int A[] = { 5, 6, 2, 8 };
int N = sizeof (A) / sizeof (A[0]);
cout << findEvenPair(A, N) << endl;
return 0;
}
|
Java
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++)
{
if ((A[i] | A[j]) % 2 == 0 )
evenPair++;
}
}
return evenPair;
}
public static void main (String[] args)
{
int A[] = { 5 , 6 , 2 , 8 };
int N = A.length;
System.out.println(findEvenPair(A, N));
}
}
|
Python 3
def findEvenPair(A, N) :
evenPair = 0
for i in range (N) :
for j in range (i + 1 , N):
if (A[i] | A[j]) % 2 = = 0 :
evenPair + = 1
return evenPair
if __name__ = = "__main__" :
A = [ 5 , 6 , 2 , 8 ]
N = len (A)
print (findEvenPair(A, N))
|
C#
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++)
{
if ((A[i] | A[j]) % 2 == 0)
evenPair++;
}
}
return evenPair;
}
public static void Main ()
{
int []A = { 5, 6, 2, 8 };
int N = A.Length;
Console.WriteLine(findEvenPair(A, N));
}
}
|
PHP
<?php
function findEvenPair( $A , $N )
{
$evenPair = 0;
for ( $i = 0; $i < $N ; $i ++)
{
for ( $j = $i + 1; $j < $N ; $j ++)
{
if (( $A [ $i ] | $A [ $j ]) % 2 == 0)
$evenPair ++;
}
}
return $evenPair ;
}
$A = array (5, 6, 2, 8);
$N = count ( $A );
echo findEvenPair( $A , $N );
?>
|
Javascript
<script>
function findEvenPair(A, N)
{
let evenPair = 0;
for (let i = 0; i < N; i++) {
for (let j = i + 1; j < N; j++) {
if ((A[i] | A[j]) % 2 == 0)
evenPair++;
}
}
return evenPair;
}
let A = [5, 6, 2, 8 ];
let N = A.length;
document.write(findEvenPair(A, N));
</script>
|
Complexity Analysis:
Time Complexity: O(N^2), since two nested loops are used.
Auxiliary Space: O(1), as constant extra space used by the algorithm.
Implementation: 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++
#include <iostream>
using namespace std;
int findEvenPair( int A[], int N)
{
int count = 0;
for ( int i = 0; i < N; i++)
if (!(A[i] & 1))
count++;
return count * (count - 1) / 2;
}
int main()
{
int A[] = { 5, 6, 2, 8 };
int N = sizeof (A) / sizeof (A[0]);
cout << findEvenPair(A, N) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int findEvenPair( int A[], int N)
{
int count = 0 ;
for ( int i = 0 ; i < N; i++)
if ((!((A[i] & 1 ) > 0 )))
count++;
return count * (count - 1 ) / 2 ;
}
public static void main (String[] args)
{
int A[] = { 5 , 6 , 2 , 8 };
int N = A.length;
System.out.println(findEvenPair(A, N));
}
}
|
Python 3
def findEvenPair(A, N):
count = 0
for i in range (N):
if ( not (A[i] & 1 )):
count + = 1
return count * (count - 1 ) / / 2
if __name__ = = "__main__" :
A = [ 5 , 6 , 2 , 8 ]
N = len (A)
print (findEvenPair(A, N))
|
C#
using System;
class GFG
{
static int findEvenPair( int []A, int N)
{
int count = 0;
for ( int i = 0; i < N; i++)
if ((!((A[i] & 1) > 0)))
count++;
return count * (count - 1) / 2;
}
public static void Main (String[] args)
{
int []A = { 5, 6, 2, 8 };
int N = A.Length;
Console.WriteLine(findEvenPair(A, N));
}
}
|
PHP
<?php
function findEvenPair(& $A , $N )
{
$count = 0;
for ( $i = 0; $i < $N ; $i ++)
if (!( $A [ $i ] & 1))
$count ++;
return $count * ( $count - 1) / 2;
}
$A = array (5, 6, 2, 8 );
$N = sizeof( $A );
echo findEvenPair( $A , $N ). "\n" ;
?>
|
Javascript
<script>
function findEvenPair(A,N)
{
let count = 0;
for (let i = 0; i < N; i++)
if ((!((A[i] & 1) > 0)))
count++;
return count * (count - 1) / 2;
}
let A=[5, 6, 2, 8 ];
let N = A.length;
document.write(findEvenPair(A, N));
</script>
|
Complexity Analysis:
Time Complexity: O(N), where N is the size of the given array.
Auxiliary Space: O(1), as constant extra space used by the algorithm.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!