Tutorialarraycounting
How Many Evens?
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
Given an integer array nums, return how many even numbers it contains. 0 and negative even numbers
count as even.
Examples
| nums | result |
|---|---|
[1, 2, 3, 4, 6] | 3 |
[1, 3, 5] | 0 |
[0, -2, -3] | 2 |
Example 1 — 2, 4 and 6 are even.
Example 2 — No even numbers.
Example 3 — 0 and -2 are even.
This is a tutorial problem: there are no performance tests, just return the correct value.
Function signature
solution(nums: int[]) → int
Constraints
-
nums.length -
nums[i]
Expected complexity
- Time
- O(N)
- Space
- O(1)
Examples
| nums | Expected |
|---|---|
[1,2,3,4,6] | 3 |
[1,3,5] | 0 |
[0,-2,-3] | 2 |
Hint
Hint
Keep count = 0 and add 1 whenever x % 2 == 0. Compare with == 0, not == 1: in several languages a
negative odd number has remainder -1.
Time limits per language
| Language | Efficiency time limit | |
|---|---|---|
| C (GCC, gnu17) | 200 ms | estimated |
| C++ (GCC, gnu++23) | 200 ms | derived |
| Python 3.13 | 1000 ms | derived |
| Java 25 | 1000 ms | derived |
| Kotlin 2.4 (JVM) | 1200 ms | estimated |
| C# 14 (.NET 10) | 1200 ms | estimated |
Test groups
| Group | Weight | Tests | Visibility |
|---|---|---|---|
| sample | 0 | 3 | inputs and outputs |
| correctness | 100 | 11 | verdict only |