|
U
Pro18. Count and Sum of Even Elements
Easy
Problem Description
Given an array of N integers, count the number of even integers present in the array and compute their sum.
An integer x is even if x(mod2)=0.
Input Format
- •Line 1: An integer N (0≤N≤105).
- •Line 2: N space-separated integers A1,A2,…,AN. (If N=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=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^9Time 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
Code
|Loading Code Editor...
SavedLn 1, Col 1
param_1 =
param_2 =
Sandboxed Engine
Ctrl+Enter = Run | Ctrl+Shift+Enter = Submit