A Naive Approach is to check for every pair and print the count of pairs which are even.
Below is the implementation of the above approach:
C++
// C++ program to count pair with
// bitwise-AND as even number
#include <iostream>
usingnamespacestd;
// Function to count number of pairs EVEN bitwise AND
intfindevenPair(intA[], intN)
{
inti, j;
// variable for counting even pairs
intevenPair = 0;
// find all pairs
for(i = 0; i < N; i++) {
for(j = i + 1; j < N; j++) {
// find AND operation
// to check evenpair
if((A[i] & A[j]) % 2 == 0)
evenPair++;
}
}
// return number of even pair
returnevenPair;
}
// Driver Code
intmain()
{
inta[] = { 5, 1, 3, 2 };
intn = sizeof(a) / sizeof(a[0]);
cout << findevenPair(a, n) << endl;
return0;
}
C
// C program to count pair with
// bitwise-AND as even number
#include <stdio.h>
// Function to count number of pairs EVEN bitwise AND
intfindevenPair(intA[], intN)
{
inti, j;
// variable for counting even pairs
intevenPair = 0;
// find all pairs
for(i = 0; i < N; i++) {
for(j = i + 1; j < N; j++) {
// find AND operation
// to check evenpair
if((A[i] & A[j]) % 2 == 0)
evenPair++;
}
}
// return number of even pair
returnevenPair;
}
// Driver Code
intmain()
{
inta[] = { 5, 1, 3, 2 };
intn = sizeof(a) / sizeof(a[0]);
printf("%d\n",findevenPair(a, n));
return0;
}
// This code is contributed by kothavvsaakash.
Java
// Java program to count pair with
// bitwise-AND as even number
importjava.io.*;
classGFG
{
// Function to count number of
// pairs EVEN bitwise AND
staticintfindevenPair(int[]A,
intN)
{
inti, j;
// variable for counting even pairs
intevenPair = 0;
// find all pairs
for(i = 0; i < N; i++)
{
for(j = i + 1; j < N; j++)
{
// find AND operation
// to check evenpair
if((A[i] & A[j]) % 2== 0)
evenPair++;
}
}
// return number of even pair
returnevenPair;
}
// Driver Code
publicstaticvoidmain (String[] args)
{
int[]a = { 5, 1, 3, 2};
intn = a.length;
System.out.println(findevenPair(a, n));
}
}
// This code is contributed by anuj_67..
Python3
# Python 3 program to count pair with
# bitwise-AND as even number
# Function to count number of
# pairs EVEN bitwise AND
deffindevenPair(A, N):
# variable for counting even pairs
evenPair =0
# find all pairs
fori inrange(0, N):
forj inrange(i +1, N):
# find AND operation to
# check evenpair
if((A[i] & A[j]) %2==0):
evenPair +=1
# return number of even pair
returnevenPair
# Driver Code
a =[ 5, 1, 3, 2]
n =len(a)
print(findevenPair(a, n))
# This code is contributed
# by PrinciRaj1992
C#
// C# program to count pair with
// bitwise-AND as even number
usingSystem;
classGFG
{
// Function to count number of
// pairs EVEN bitwise AND
staticintfindevenPair(int[]A,
intN)
{
inti, j;
// variable for counting even pairs
intevenPair = 0;
// find all pairs
for(i = 0; i < N; i++)
{
for(j = i + 1; j < N; j++)
{
// find AND operation
// to check evenpair
if((A[i] & A[j]) % 2 == 0)
evenPair++;
}
}
// return number of even pair
returnevenPair;
}
// Driver Code
publicstaticvoidMain ()
{
int[]a = { 5, 1, 3, 2 };
intn = a.Length;
Console.WriteLine(findevenPair(a, n));
}
}
// This code is contributed by anuj_67..
PHP
<?php
// PHP program to count pair with
// bitwise-AND as even number
// Function to count number of
// pairs EVEN bitwise AND
functionfindevenPair($A, $N)
{
// variable for counting even pairs
$evenPair= 0;
// find all pairs
for($i= 0; $i< $N; $i++)
{
for($j= $i+ 1; $j< $N; $j++)
{
// find AND operation
// to check evenpair
if(($A[$i] & $A[$j]) % 2 == 0)
$evenPair++;
}
}
// return number of even pair
return$evenPair;
}
// Driver Code
$a= array(5, 1, 3, 2 );
$n= sizeof($a);
echofindevenPair($a, $n);
// This code is contributed by akt_mit
?>
Javascript
<script>
// Javascript program to count pair with
// bitwise-AND as even number
// Function to count number of pairs EVEN bitwise AND
functionfindevenPair(A, N)
{
let i, j;
// variable for counting even pairs
let evenPair = 0;
// find all pairs
for(i = 0; i < N; i++) {
for(j = i + 1; j < N; j++) {
// find AND operation
// to check evenpair
if((A[i] & A[j]) % 2 == 0)
evenPair++;
}
}
// return number of even pair
returnevenPair;
}
// Driver Code
let a = [ 5, 1, 3, 2 ];
let n = a.length;
document.write(findevenPair(a, n));
// This code is contributed by souravmahato348.
</script>
Output
3
Time Complexity: O(N2) Auxiliary Space: O(1)
An Efficient Approach will be to observe that the bitwise AND of two numbers will be even if atleast one of the two numbers is even. So, count all the odd numbers in the array say count. Now, number of pairs with both odd elements will be count*(count-1)/2.
Therefore, number of elements with atleast one even element will be:
Total Pairs - Count of pair with both odd elements
Below is the implementation of the above approach:
C++
// C++ program to count pair with
// bitwise-AND as even number
#include <iostream>
usingnamespacestd;
// Function to count number of pairs
// with EVEN bitwise AND
intfindevenPair(intA[], intN)
{
intcount = 0;
// count odd numbers
for(inti = 0; i < N; i++)
if(A[i] % 2 != 0)
count++;
// count odd pairs
intoddCount = count * (count - 1) / 2;
// return number of even pair
return(N * (N - 1) / 2) - oddCount;
}
// Driver Code
intmain()
{
inta[] = { 5, 1, 3, 2 };
intn = sizeof(a) / sizeof(a[0]);
cout << findevenPair(a, n) << endl;
return0;
}
C
// C program to count pair with
// bitwise-AND as even number
#include <stdio.h>
// Function to count number of pairs
// with EVEN bitwise AND
intfindevenPair(intA[], intN)
{
intcount = 0;
// count odd numbers
for(inti = 0; i < N; i++)
if(A[i] % 2 != 0)
count++;
// count odd pairs
intoddCount = count * (count - 1) / 2;
// return number of even pair
return(N * (N - 1) / 2) - oddCount;
}
// Driver Code
intmain()
{
inta[] = { 5, 1, 3, 2 };
intn = sizeof(a) / sizeof(a[0]);
printf("%d\n",findevenPair(a, n));
return0;
}
// This code is contributed by kothavvsaakash.
Java
// Java program to count pair with
// bitwise-AND as even number
importjava.io.*;
classGFG {
// Function to count number of pairs
// with EVEN bitwise AND
staticintfindevenPair(intA[], intN)
{
intcount = 0;
// count odd numbers
for(inti = 0; i < N; i++)
if(A[i] % 2!= 0)
count++;
// count odd pairs
intoddCount = count * (count - 1) / 2;
// return number of even pair
return(N * (N - 1) / 2) - oddCount;
}
// Driver Code
publicstaticvoidmain (String[] args) {
inta[] = { 5, 1, 3, 2};
intn =a.length;
System.out.print( findevenPair(a, n));
}
}
// This code is contributed by anuj_67..
Python3
# Python 3 program to count pair with
# bitwise-AND as even number
# Function to count number of pairs
# with EVEN bitwise AND
deffindevenPair(A, N):
count =0
# count odd numbers
fori inrange(0, N):
if(A[i] %2!=0):
count +=1
# count odd pairs
oddCount =count *(count -1) /2
# return number of even pair
return(int)((N *(N -1) /2) -oddCount)
# Driver Code
a =[5, 1, 3, 2]
n =len(a)
print(findevenPair(a, n))
# This code is contributed
# by PrinciRaj1992
C#
// C# program to count pair with
// bitwise-AND as even number
usingSystem;
publicclassGFG{
// Function to count number of pairs
// with EVEN bitwise AND
staticintfindevenPair(int[]A, intN)
{
intcount = 0;
// count odd numbers
for(inti = 0; i < N; i++)
if(A[i] % 2 != 0)
count++;
// count odd pairs
intoddCount = count * (count - 1) / 2;
// return number of even pair
return(N * (N - 1) / 2) - oddCount;
}
// Driver Code
staticpublicvoidMain (){
int[]a = { 5, 1, 3, 2 };
intn =a.Length;
Console.WriteLine( findevenPair(a, n));
}
}
// This code is contributed by ajit
PHP
<?php
// PHP program to count pair with
// bitwise-AND as even number
// Function to count number of pairs
// with EVEN bitwise AND
functionfindevenPair($A, $N)
{
$count= 0;
// count odd numbers
for($i= 0; $i< $N; $i++)
if($A[$i] % 2 != 0)
$count++;
// count odd pairs
$oddCount= $count* ($count- 1) / 2;
// return number of even pair
return($N* ($N- 1) / 2) - $oddCount;
}
// Driver Code
$a= array(5, 1, 3, 2);
$n= sizeof($a);
echofindevenPair($a, $n) . "\n";
// This code is contributed
// by Akanksha Rai
?>
Javascript
<script>
// Javascript program to count pair with
// bitwise-AND as even number
// Function to count number of pairs
// with EVEN bitwise AND
functionfindevenPair(A, N)
{
let count = 0;
// Count odd numbers
for(let i = 0; i < N; i++)
if(A[i] % 2 != 0)
count++;
// Count odd pairs
let oddCount = parseInt((count *
(count - 1)) / 2);
// Return number of even pair
returnparseInt((N * (N - 1)) / 2) - oddCount;
}
// Driver Code
let a = [ 5, 1, 3, 2 ];
let n = a.length;
document.write(findevenPair(a, n));
// This code is contributed by subhammahato348
</script>
Output
3
Time Complexity: O(N) Auxiliary Space: O(1)
Another Method: we observe that bitwise AND of any two numbers will be even if any one of them will be even, no matter what is another number, if one number is even bitwise ANd of that number with any number will be even.
so if our array has an even number count as evenNumCount then the total no. of pair whose bitwise AND is even will be
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!