#C2598. Even Sum Pairs
Even Sum Pairs
Even Sum Pairs
Given an array of n integers, you are to count the number of pairs (i, j)
where i < j
such that the sum of the elements at indices i
and j
is even. The sum of two integers is even if either both of them are even or both of them are odd. Formally, you need to find the number of pairs \( (i, j) \) satisfying \( 0 \leq i < j < n \) for which
[ (array[i] + array[j]) \mod 2 = 0 ]
Example:
- For
n = 5
andarray = [1, 2, 3, 4, 5]
, the valid pairs are such that the sum is even, and the answer is 4. - For
n = 3
andarray = [2, 4, 6]
, all pairs produce an even sum and the answer is 3.
Your task is to implement a function that reads an integer n
followed by n
integers from standard input and outputs a single integer representing the number of valid pairs.
inputFormat
The input is given via standard input. The first line contains a single integer n
(\( 0 \leq n \leq 10^5 \)), representing the number of elements in the array. The second line contains \( n \) space-separated integers, each representing an element of the array.
outputFormat
Output a single integer representing the number of pairs \( (i, j) \) (with \( i < j \)) for which the sum of the elements is even.
## sample5
1 2 3 4 5
4