Count Pairs Odd XOR

Last Updated : 26 Jul, 2026

Given an integer array arr[], determine the number of pairs (i, j) such that 0 ≤ i < j < arr.size() and the XOR of arr[i] and arr[j] is odd.

Examples : 

Input: arr[] = [1, 2, 3]
Output: 2
Explanation:
The possible pairs are:
(1, 2) -> 1 ^ 2 = 3 (odd)
(1, 3) -> 1 ^ 3 = 2 (even)
(2, 3) -> 2 ^ 3 = 1 (odd)
Hence, there are 2 pairs whose XOR is odd.

Input: arr[] = [1, 2]
Output: 1
Explanation:
The only possible pair is:
(1, 2) -> 1 ^ 2 = 3 (odd)
Therefore, the number of pairs with odd XOR is 1.

Try It Yourself
redirect icon

[Naive Approach] Check XOR of Every Pair - O(n ^ 2) Time and O(1) Space

The idea is to generate all possible pairs of elements and compute their XOR. If the XOR value is odd, increment the count. Finally, return the total count of such pairs.

Working of Approach:

  • Initialize a variable count to store the answer.
  • Traverse all pairs (i, j) where i < j.
  • Compute arr[i] ^ arr[j].
  • If the XOR result is odd, increment count.
C++
#include <iostream>
#include <vector>
using namespace std;

int countXorPair(vector<int> &arr)
{
    int n = arr.size();
    int count = 0;

    // Check every possible pair
    for (int i = 0; i < n; i++)
    {
        for (int j = i + 1; j < n; j++)
        {

            // If XOR is odd, increment count
            if ((arr[i] ^ arr[j]) & 1)
                count++;
        }
    }

    return count;
}

int main()
{
    vector<int> arr = {1, 2};

    cout << countXorPair(arr);

    return 0;
}
Java
import java.util.Arrays;

public class GFG {
    public static int countXorPair(int[] arr)
    {
        int n = arr.length;
        int count = 0;

        // Check every possible pair
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {

                // If XOR is odd, increment count
                if ((arr[i] ^ arr[j]) % 2 == 1)
                    count++;
            }
        }

        return count;
    }

    public static void main(String[] args)
    {
        int[] arr = { 1, 2 };

        System.out.println(countXorPair(arr));
    }
}
Python
def countXorPair(arr):
    n = len(arr)
    count = 0

    # Check every possible pair
    for i in range(n):
        for j in range(i + 1, n):

            # If XOR is odd, increment count
            if (arr[i] ^ arr[j]) & 1:
                count += 1

    return count


if __name__ == '__main__':
    arr = [1, 2]

    print(countXorPair(arr))
C#
using System;

public class GFG {
    public static int countXorPair(int[] arr)
    {
        int n = arr.Length;
        int count = 0;

        // Check every possible pair
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {

                // If XOR is odd, increment count
                if ((arr[i] ^ arr[j]) % 2 == 1)
                    count++;
            }
        }

        return count;
    }

    public static void Main()
    {
        int[] arr = { 1, 2 };

        Console.WriteLine(countXorPair(arr));
    }
}
JavaScript
function countXorPair(arr)
{
    let n = arr.length;
    let count = 0;

    // Check every possible pair
    for (let i = 0; i < n; i++) {
        for (let j = i + 1; j < n; j++) {

            // If XOR is odd, increment count
            if ((arr[i] ^ arr[j]) & 1)
                count++;
        }
    }

    return count;
}

// Driver Code
let arr = [ 1, 2 ];
console.log(countXorPair(arr));

Output
1

[Expected Approach] Count Odd and Even Elements - O(n) Time and O(1) Space

The idea is to use the property of XOR that the XOR of two numbers is odd only when one number is odd and the other is even. So, count the number of odd and even elements, and multiply these counts to get the answer.

Working of Approach:

  • Count the number of odd elements in the array.
  • Count the number of even elements in the array.
  • Every odd element forms a valid pair with every even element.
  • Compute oddCnt * evenCnt.
  • Return the result.

Let us understand with an example:
Input: arr[] = [1, 2]

  • Count the odd elements: 1 -> oddCnt = 1.
  • Count the even elements: 2 -> evenCnt = 1.
  • A pair has an odd XOR only if one element is odd and the other is even.
  • Therefore, the number of valid pairs is oddCnt × evenCnt = 1 × 1 = 1.
  • Hence, the output is 1.
C++
#include <iostream>
#include <vector>
using namespace std;

int countXorPair(vector<int> &arr)
{
    int oddCnt = 0;
    int evenCnt = 0;

    // Count the number of odd and even elements.
    for (int num : arr)
    {
        if (num & 1)
        {
            oddCnt++;
        }
        else
        {
            evenCnt++;
        }
    }

    // A pair has odd XOR if one number is odd
    // and the other number is even.
    int res = oddCnt * evenCnt;

    return res;
}

int main()
{
    vector<int> arr = {1, 2};

    cout << countXorPair(arr);

    return 0;
}
Java
import java.util.*;

class GFG {

    static int countXorPair(int[] arr)
    {
        int oddCnt = 0;
        int evenCnt = 0;

        // Count the number of odd and even elements.
        for (int num : arr) {
            if ((num & 1) == 1) {
                oddCnt++;
            }
            else {
                evenCnt++;
            }
        }

        // A pair has odd XOR if one number is odd
        // and the other number is even.
        int res = oddCnt * evenCnt;

        return res;
    }

    public static void main(String[] args)
    {
        int[] arr = { 1, 2 };

        System.out.println(countXorPair(arr));
    }
}
Python
def countXorPair(arr):
    oddCnt = 0
    evenCnt = 0

    # Count the number of odd and even elements.
    for num in arr:
        if num & 1:
            oddCnt += 1
        else:
            evenCnt += 1

    # A pair has odd XOR if one number is odd
    # and the other number is even.
    res = oddCnt * evenCnt

    return res


if __name__ == '__main__':
    arr = [1, 2]

    print(countXorPair(arr))
C#
using System;

class GFG {
    static int countXorPair(int[] arr)
    {
        int oddCnt = 0;
        int evenCnt = 0;

        // Count the number of odd and even elements.
        foreach(int num in arr)
        {
            if ((num & 1) == 1) {
                oddCnt++;
            }
            else {
                evenCnt++;
            }
        }

        // A pair has odd XOR if one number is odd
        // and the other number is even.
        int res = oddCnt * evenCnt;

        return res;
    }

    static void Main()
    {
        int[] arr = { 1, 2 };

        Console.WriteLine(countXorPair(arr));
    }
}
JavaScript
function countXorPair(arr)
{
    let oddCnt = 0;
    let evenCnt = 0;

    // Count the number of odd and even elements.
    for (let num of arr) {
        if (num & 1) {
            oddCnt++;
        }
        else {
            evenCnt++;
        }
    }

    // A pair has odd XOR if one number is odd
    // and the other number is even.
    let res = oddCnt * evenCnt;

    return res;
}

// Driver Code
let arr = [ 1, 2 ];
console.log(countXorPair(arr));

Output
1
Comment