Given an array of n positive numbers, the task is to count number of ordered pairs with even and odd sum.
Input: arr[] = {1, 2, 4}
Output: Even sum Pairs = 2, Odd sum Pairs = 4
The ordered pairs are (1, 2), (1, 4), (2, 1), (4, 1), (2, 4), (4, 2)
Pairs with Even sum: (2, 4), (4, 2)
Pairs with Odd sum: (1, 2), (1, 4), (2, 1), (4, 1)
Input: arr[] = {2, 4, 5, 9, 1, 8}
Output: Even sum Pairs = 12, Odd sum Pairs = 18
The sum of two numbers is odd if one number is odd and another one is even. So now we have to count the even and odd numbers. As in order pair (a, b) and (b, a) both treated as different pair, therefore
This is because every even number can pair with every odd number, and every odd number can pair with every even number. Thus multiply 2 is done in the answer.
And the number of even sum pairs will be an inversion of the number of odd sum pairs. Therefore: