![]() |
VOOZH | about |
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
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:
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:
2
Time Complexity : O(n)
Space Complexity : O(1)