Even Pair Sum
easySave
ArrayCountingHash Table
Given an array of integers, count how many pairs (i, j) (where i < j) have an even sum. Return the count of such pairs.
Example 1
Input
[ 2, 4, 1, 3 ]
Output
3
Explanation. Pairs with even sum are: (2+4), (2+2), and (4+4).
Example 2
Input
[ 1, 3, 5, 7 ]
Output
0
Explanation. There are no pairs with even sum because all elements are odd.
Example 3
Input
[ 1, 2, 3, 4 ]
Output
2
Explanation. Pairs with even sum are: (1+3) and (2+4).
Example 4
Input
[ 10, 20, 30 ]
Output
3
Explanation. Every pair among the three elements forms an even sum.
Follow-up: Can you optimize your solution to O(n) time complexity?
Constraints:
- The array will contain at least two integers. - Each integer will be between -10,000 and 10,000.
- Views
- 2