Tutorialmath
Is It Even?
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
Given an integer n, return true if n is even and false if it is odd. Even numbers are divisible by
2; 0 and negative numbers such as -4 are even too.
Examples
| n | result |
|---|---|
4 | true |
7 | false |
-6 | true |
Example 1 — 4 is divisible by 2.
Example 2 — 7 leaves a remainder of 1.
Example 3 — -6 is divisible by 2, so it is even.
This is a tutorial problem: there are no performance tests, just return the correct value.
Function signature
solution(n: int) → bool
Constraints
-
n
Expected complexity
- Time
- O(1)
- Space
- O(1)
Examples
| n | Expected |
|---|---|
4 | true |
7 | false |
-6 | true |
Hint
Hint
n % 2 is the remainder after dividing by 2; the number is even when it is 0. In C, C++, Java and C# the
remainder of a negative number is negative (-3 % 2 == -1), so test n % 2 == 0 rather than n % 2 == 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 | 13 | verdict only |