Tutorialmath
Absolute Value
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
Return the absolute value of the integer n: the number without its sign, i.e. its distance from 0. For
example, the absolute value of -8 is 8 and that of 5 is 5.
Examples
| n | result |
|---|---|
-8 | 8 |
5 | 5 |
0 | 0 |
Example 1 — Drop the sign: 8.
Example 2 — A positive number stays the same.
Example 3 — The absolute value of 0 is 0.
This is a tutorial problem: there are no performance tests, just return the correct value.
Function signature
solution(n: int) → int
Constraints
-
n
Expected complexity
- Time
- O(1)
- Space
- O(1)
Examples
| n | Expected |
|---|---|
-8 | 8 |
5 | 5 |
0 | 0 |
Hint
Hint
If n < 0 return -n, otherwise return n.
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 | 12 | verdict only |