Funcia
Tutorialmath

Is It Even?

Memory limit
256 MB
Correctness time limit
10000 ms
Solvers
0
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

nresult
4true
7false
-6true

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

  • −109≤-10^9 \le n ≤109\le 10^9

Expected complexity

Time
O(1)
Space
O(1)

Examples

nExpected
4true
7false
-6true

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

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
correctness10013verdict only
Loading editor…
Log in to run or submit code.

No result yet. Run or submit your code.