Funcia
Tutorialmath

Absolute Value

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

nresult
-88
55
00

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

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

Expected complexity

Time
O(1)
Space
O(1)

Examples

nExpected
-88
55
00

Hint

Hint

If n < 0 return -n, otherwise return n.

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

No result yet. Run or submit your code.