Count pairs with Bitwise-AND as even number - GeeksforGeeks

22 Apr, 2021
Given an array of integers. The task is to find the number of pairs (i, j) such that A[i] & A[j] is even.
Examples: 
Input: N = 4, A[] = { 5, 1, 3, 2 }
Output: 3
Since pair of A[] are:
( 5, 1 ), ( 5, 3 ), ( 5, 2 ), ( 1, 3 ), ( 1, 2 ), ( 3, 2 )
5 AND 1 = 1, 5 AND 3 = 1, 5 AND 2 = 0,
1 AND 3 = 1, 1 AND 2 = 0,
3 AND 2 = 2
Total even pair A( i, j ) = 3

Input : N = 6, A[] = { 5, 9, 0, 6, 7, 3 }
Output : 9
Since pair of A[] =
( 5, 9 ) = 1, ( 5, 0 ) = 0, ( 5, 6 ) = 4, ( 5, 7 ) = 5, ( 5, 3 ) = 1,
( 9, 0 ) = 0, ( 9, 6 ) = 0, ( 9, 7 ) = 1, ( 9, 3 ) = 1,
( 0, 6 ) = 0, ( 0, 7 ) = 0, ( 0, 3 ) = 0,
( 6, 7 ) = 6, ( 6, 3 ) = 2,
( 7, 3 ) = 3
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++
Java
Python3
C#
PHP
Javascript
// C++ program to count pair with// bitwise-AND as even number #include <iostream>using namespace std; // Function to count number of pairs EVEN bitwise ANDint findevenPair(int A[], int N){    int i, j;     // variable for counting even pairs    int 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 Codeint main(){     int a[] = { 5, 1, 3, 2 };    int n = sizeof(a) / sizeof(a[0]);     cout << findevenPair(a, n) << endl;     return 0;}
// Java program to count pair with// bitwise-AND as even numberimport java.io.*; class GFG{ // Function to count number of// pairs EVEN bitwise ANDstatic int findevenPair(int []A,                        int N){    int i, j;     // variable for counting even pairs    int 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 Codepublic static void main (String[] args){    int []a = { 5, 1, 3, 2 };    int n = a.length;         System.out.println(findevenPair(a, n));}} // This code is contributed by anuj_67..
# Python 3 program to count pair with# bitwise-AND as even number # Function to count number of# pairs EVEN bitwise ANDdef findevenPair(A, N):     # variable for counting even pairs    evenPair = 0     # find all pairs    for i in range(0, N):        for j in range(i + 1, N):                         # find AND operation to            # check evenpair            if ((A[i] & A[j]) % 2 == 0):                evenPair += 1     # return number of even pair    return evenPair # Driver Codea = [ 5, 1, 3, 2 ]n = len(a) print(findevenPair(a, n)) # This code is contributed# by PrinciRaj1992
// C# program to count pair with// bitwise-AND as even numberusing System; class GFG{ // Function to count number of// pairs EVEN bitwise ANDstatic int findevenPair(int []A,                        int N){    int i, j;     // variable for counting even pairs    int 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 Codepublic static void Main (){    int []a = { 5, 1, 3, 2 };    int n = a.Length;         Console.WriteLine(findevenPair(a, n));}} // This code is contributed by anuj_67..
<?php// PHP program to count pair with// bitwise-AND as even number // Function to count number of// pairs EVEN bitwise ANDfunction findevenPair($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);echo findevenPair($a, $n); // This code is contributed by akt_mit?>
<script>// Javascript program to count pair with// bitwise-AND as even number // Function to count number of pairs EVEN bitwise ANDfunction findevenPair(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    return evenPair;} // Driver Code let a = [ 5, 1, 3, 2 ];let n = a.length; document.write(findevenPair(a, n)); // This code is contributed by souravmahato348.</script>
3
 
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++
Java
Python3
C#
PHP
Javascript
// C++ program to count pair with// bitwise-AND as even number #include <iostream>using namespace std; // Function to count number of pairs// with EVEN bitwise ANDint findevenPair(int A[], int N){    int count = 0;     // count odd numbers    for (int i = 0; i < N; i++)        if (A[i] % 2 != 0)            count++;     // count odd pairs    int oddCount = count * (count - 1) / 2;     // return number of even pair    return (N * (N - 1) / 2) - oddCount;} // Driver Codeint main(){     int a[] = { 5, 1, 3, 2 };    int n = sizeof(a) / sizeof(a[0]);     cout << findevenPair(a, n) << endl;     return 0;}
// Java program to count pair with// bitwise-AND as even number  import java.io.*; class GFG {    // Function to count number of pairs// with EVEN bitwise ANDstatic int findevenPair(int A[], int N){    int count = 0;     // count odd numbers    for (int i = 0; i < N; i++)        if (A[i] % 2 != 0)            count++;     // count odd pairs    int oddCount = count * (count - 1) / 2;     // return number of even pair    return (N * (N - 1) / 2) - oddCount;} // Driver Code     public static void main (String[] args) {            int a[] = { 5, 1, 3, 2 };    int n =a.length;     System.out.print( findevenPair(a, n));    }}// This code is contributed by anuj_67..
# Python 3 program to count pair with# bitwise-AND as even number # Function to count number of pairs# with EVEN bitwise ANDdef findevenPair(A, N):    count = 0     # count odd numbers    for i in range(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 Codea = [5, 1, 3, 2 ]n = len(a) print(findevenPair(a, n)) # This code is contributed# by PrinciRaj1992
// C# program to count pair with// bitwise-AND as even number using System; public class GFG{     // Function to count number of pairs// with EVEN bitwise ANDstatic int findevenPair(int []A, int N){    int count = 0;     // count odd numbers    for (int i = 0; i < N; i++)        if (A[i] % 2 != 0)            count++;     // count odd pairs    int oddCount = count * (count - 1) / 2;     // return number of even pair    return (N * (N - 1) / 2) - oddCount;} // Driver Code     static public void Main (){    int []a = { 5, 1, 3, 2 };    int n =a.Length;    Console.WriteLine( findevenPair(a, n));    }}// This code is contributed by ajit
<?php// PHP program to count pair with// bitwise-AND as even number // Function to count number of pairs// with EVEN bitwise ANDfunction findevenPair($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); echo findevenPair($a, $n) . "\n"; // This code is contributed// by Akanksha Rai?>
<script> // Javascript program to count pair with// bitwise-AND as even number // Function to count number of pairs// with EVEN bitwise ANDfunction findevenPair(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    return parseInt((N * (N - 1)) / 2) - oddCount;} // Driver Codelet a = [ 5, 1, 3, 2 ];let n = a.length; document.write(findevenPair(a, n)); // This code is contributed by subhammahato348 </script>
3
 
Time Complexity: O(N) 
inderDuMCA
Shashank12
jit_t
princiraj1992
Akanksha_Rai
souravmahato348
subhammahato348
Bitwise-AND
math
Technical Scripter 2018
Algorithms
Bit Magic
Technical Scripter
Bit Magic
Algorithms
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
SDE SHEET - A Complete Guide for SDE Preparation
DSA Sheet by Love Babbar
Introduction to Algorithms
Playfair Cipher with Examples
How to write a Pseudo Code?
Bitwise Operators in C/C++
Left Shift and Right Shift Operators in C/C++
Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
Cyclic Redundancy Check and Modulo-2 Division
How to swap two numbers without using a temporary variable?