# Count pairs with Bitwise XOR as EVEN number

• Last Updated : 28 Apr, 2021

Given an array of N integers, the task is to find the number of pairs (i, j) such that A[i] ^ A[j] is even.
Examples:

```Input: A[] =  { 5, 4, 7, 2, 1}
Output: 4
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 even pair  = 4

Input: A[] = { 7, 2, 8, 1, 0, 5, 11 }
Output: 9
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```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

A naive approach is to check for every pair and print the count of pairs that are even.
Below is the implementation of the above approach:

## C++

 `// C++ program to count pairs``// with XOR giving a even number``#include ``using` `namespace` `std;` `// Function to count number of even pairs``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 XOR operation``            ``// check even or even``            ``if` `((A[i] ^ A[j]) % 2 == 0)``                ``evenPair++;``        ``}``    ``}` `    ``// return number of even pair``    ``return` `evenPair;``}` `// Driver Code``int` `main()``{` `    ``int` `A[] = { 5, 4, 7, 2, 1 };``    ``int` `N = ``sizeof``(A) / ``sizeof``(A);` `    ``// calling function findevenPair``    ``// and print number of even pair``    ``cout << findevenPair(A, N) << endl;` `    ``return` `0;``}`

## Java

 `// Java program to count pairs``// with XOR giving a even number``import` `java.io.*;` `class` `GFG``{` `// Function to count number of even pairs``static` `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 XOR operation``            ``// check even or even``            ``if` `((A[i] ^ A[j]) % ``2` `== ``0``)``                ``evenPair++;``        ``}``    ``}` `    ``// return number of even pair``    ``return` `evenPair;``}` `// Driver Code``public` `static` `void` `main (String[] args)``{``    ``int` `A[] = { ``5``, ``4``, ``7``, ``2``, ``1` `};``    ``int` `N = A.length;``    ` `    ``// calling function findevenPair``    ``// and print number of even pair``    ``System.out.println(findevenPair(A, N));``}``}` `// This code is contributed by inder_verma..`

## Python3

 `    ` `# Python3 program to count pairs``# with XOR giving a even number` ` ` `# Function to count number of even pairs``def` `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 XOR operation``            ``# check even or even``            ``if` `((A[i] ^ A[j]) ``%` `2` `=``=` `0``):``                ``evenPair``+``=``1` `    ``# return number of even pair``    ``return` `evenPair;`` ` `# Driver Code``def` `main():``    ``A ``=` `[ ``5``, ``4``, ``7``, ``2``, ``1` `]``    ``N ``=` `len``(A)`` ` `    ``# calling function findevenPair``    ``# and prnumber of even pair``    ``print``(findevenPair(A, N))`` ` `if` `__name__ ``=``=` `'__main__'``:``    ``main()``# This code is contributed by PrinciRaj1992`

## C#

 `// C# program to count pairs``// with XOR giving a even number``using` `System;` `class` `GFG``{` `// Function to count number of``// even pairs``static` `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 XOR operation``            ``// check even or even``            ``if` `((A[i] ^ A[j]) % 2 == 0)``                ``evenPair++;``        ``}``    ``}` `    ``// return number of even pair``    ``return` `evenPair;``}` `// Driver Code``public` `static` `void` `Main ()``{``    ``int` `[]A = { 5, 4, 7, 2, 1 };``    ``int` `N = A.Length;``    ` `    ``// calling function findevenPair``    ``// and print number of even pair``    ``Console.WriteLine(findevenPair(A, N));``}``}` `// This code is contributed``// by inder_verma..`

## PHP

 ``

## Javascript

 ``
Output:
`4`

Time Complexity: O(n^2)
An efficient solution is to Count pairs with Bitwise XOR as ODD number i.e. oddEvenpairs. Then return totalPairs – oddEvenPairs where totalPairs = (N * (N-1) / 2) and oddEvenPairs = count * (N – count). As, pairs that will give Even Bitwise XOR are :

Even, Even
Odd, Odd

So, find the count of pairs with both odd and even elements and subtract from total no. of pairs.
Below is the implementation of the above approach:

## C++

 `// C++ program to count pairs``// with XOR giving a even number``#include ``using` `namespace` `std;` `// Function to count number of even pairs``int` `findEvenPair(``int` `A[], ``int` `N)``{``    ``int` `count = 0;` `    ``// find all pairs``    ``for` `(``int` `i = 0; i < N; i++) {``        ``if` `(A[i] % 2 != 0)``            ``count++;``    ``}` `    ``int` `totalPairs = (N * (N - 1) / 2);``    ``int` `oddEvenPairs = count * (N - count);` `    ``// return number of even pair``    ``return` `totalPairs - oddEvenPairs;``}` `// Driver Code``int` `main()``{``    ``int` `a[] = { 5, 4, 7, 2, 1 };``    ``int` `n = ``sizeof``(a) / ``sizeof``(a);` `    ``// calling function findEvenPair``    ``// and print number of even pair``    ``cout << findEvenPair(a, n) << endl;` `    ``return` `0;``}`

## Java

 `// Java  program to count pairs``// with XOR giving a even number` `import` `java.io.*;` `class` `GFG {``    ``// Function to count number of even pairs``static` `int` `findEvenPair(``int` `A[], ``int` `N)``{``    ``int` `count = ``0``;` `    ``// find all pairs``    ``for` `(``int` `i = ``0``; i < N; i++) {``        ``if` `(A[i] % ``2` `!= ``0``)``            ``count++;``    ``}` `    ``int` `totalPairs = (N * (N - ``1``) / ``2``);``    ``int` `oddEvenPairs = count * (N - count);` `    ``// return number of even pair``    ``return` `totalPairs - oddEvenPairs;``}` `// Driver Code``    ` `    ``public` `static` `void` `main (String[] args) {``    ` `    ``int` `a[] = { ``5``, ``4``, ``7``, ``2``, ``1` `};``    ``int` `n = a.length;``    ``// calling function findEvenPair``    ``// and print number of even pair``    ``System.out.println(findEvenPair(a, n));``    ``}``//This code is contributed by akt_mit   ``}`

## Python3

 `    ` `# python program to count pairs``# with XOR giving a even number` `# Function to count number of even pairs``def` `findEvenPair(A, N):``    ``count ``=` `0`` ` `    ``# find all pairs``    ``for` `i ``in` `range``(``0``,N):``        ``if` `(A[i] ``%` `2` `!``=` `0``):``            ``count``+``=``1`` ` `    ``totalPairs ``=` `(N ``*` `(N ``-` `1``) ``/` `2``)``    ``oddEvenPairs ``=` `count ``*` `(N ``-` `count)`` ` `    ``# return number of even pair``    ``return` `(``int``)(totalPairs ``-` `oddEvenPairs)` `# Driver Code``def` `main():``    ``a ``=` `[ ``5``, ``4``, ``7``, ``2``, ``1` `]``    ``n ``=` `len``(a)`` ` `    ``# calling function findEvenPair``    ``# and pr number of even pair``    ``print``(findEvenPair(a, n))`` ` `if` `__name__ ``=``=` `'__main__'``:``    ``main()``    ` `# This code is contributed by 29AjayKumar`

## C#

 `    ` `// C# program to count pairs``// with XOR giving a even number`` ` `using` `System;`` ` `public` `class` `GFG {``    ``// Function to count number of even pairs``    ``static` `int` `findEvenPair(``int` `[]A, ``int` `N)``    ``{``        ``int` `count = 0;` `        ``// find all pairs``        ``for` `(``int` `i = 0; i < N; i++) {``            ``if` `(A[i] % 2 != 0)``                ``count++;``        ``}` `        ``int` `totalPairs = (N * (N - 1) / 2);``        ``int` `oddEvenPairs = count * (N - count);` `        ``// return number of even pair``        ``return` `totalPairs - oddEvenPairs;``    ``}` `    ``// Driver Code``     ` `    ``public` `static` `void` `Main() {``     ` `    ``int` `[]a = { 5, 4, 7, 2, 1 };``    ``int` `n = a.Length;``    ``// calling function findEvenPair``    ``// and print number of even pair``    ``Console.Write(findEvenPair(a, n));``    ``}``}` `// This code is contributed by 29AjayKumar`

## PHP

 ``

## Javascript

 ``
Output:
`4`

Time Complexity : O(n)

My Personal Notes arrow_drop_up