U

18. Count and Sum of Even Elements

Easy

Problem Description

Given an array of NN integers, count the number of even integers present in the array and compute their sum.

An integer xx is even if x(mod2)=0x \pmod 2 = 0.

Input Format

  • •Line 1: An integer NN (0≤N≤1050 \le N \le 10^5).
  • •Line 2: NN space-separated integers A1,A2,…,ANA_1, A_2, \dots, A_N. (If N=0N = 0, this line may be empty).

Output Format

Print two space-separated integers: the count of even numbers followed by their total sum. If there are no even numbers (or N=0N = 0), output 0 0.

Example 1:
Input:
5
1 2 3 4 6
Output:
3 12
Explanation: Even numbers are 2, 4, and 6. Count is 3, sum is 2 + 4 + 6 = 12.
Example 2:
Input:
3
1 3 5
Output:
0 0
Explanation: No even numbers exist in the array.
Example 3:
Input:
0
Output:
0 0
Explanation: The array is empty, so count and sum are both 0.
Constraints:
  • 0 <= N <= 10^5
  • -10^9 <= A[i] <= 10^9
  • Time complexity target: O(N)
  • Auxiliary space target: O(1)
Follow up:How would you partition the array in-place so all even numbers appear before odd numbers?
1,180 Online