Funcia
Tutorialarraycounting

How Many Evens?

Memory limit
256 MB
Correctness time limit
10000 ms
Solvers
0
0

Given an integer array nums, return how many even numbers it contains. 0 and negative even numbers count as even.

Examples

numsresult
[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

  • 1≤1 \le nums.length ≤1,000\le 1{,}000
  • −106≤-10^6 \le nums[i] ≤106\le 10^6

Expected complexity

Time
O(N)
Space
O(1)

Examples

numsExpected
[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

LanguageEfficiency time limit
C (GCC, gnu17)200 msestimated
C++ (GCC, gnu++23)200 msderived
Python 3.131000 msderived
Java 251000 msderived
Kotlin 2.4 (JVM)1200 msestimated
C# 14 (.NET 10)1200 msestimated

Test groups

GroupWeightTestsVisibility
sample03inputs and outputs
correctness10011verdict only
Loading editor…
Log in to run or submit code.

No result yet. Run or submit your code.