VOOZH about

URL: https://www.geeksforgeeks.org/dsa/count-pairs-odd-xor/

⇱ Count pairs with Odd XOR - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Count pairs with Odd XOR

Last Updated : 23 Jul, 2025

Given an array of n integers. Find out number of pairs in array whose XOR is odd.

Examples : 

Input : arr[] = { 1, 2, 3 }
Output : 2
All pairs of array
1 ^ 2 = 3
1 ^ 3 = 2
2 ^ 3 = 1

Input : arr[] = { 1, 2, 3, 4 }
Output : 4
Recommended Practice

Naive Approach: We can find pairs whose XOR is odd by running two loops. If XOR of two number is odd increase count of pairs.

Implementation:


Output
2

Time Complexity : O(n*n)
Space Complexity - O(1)

Efficient Approach: We can observe that: 

odd ^ odd = even
odd ^ even = odd
even ^ odd = odd
even ^ even = even

Therefore total pairs in array whose XOR is odd will be equal to count of odd numbers multiplied by count of even numbers.

Implementation:


Output
2

Time Complexity : O(n)
Space Complexity : O(1)

Comment