easy +5 pts

Absolute Difference of Two Integers

Return the absolute difference between two integer inputs, no matter which is larger.

Write a function `absolute_difference(a, b)` that takes two integers `a` and `b` and returns the absolute value of their difference, i.e., `|a - b|`. The result must always be non-negative. **Input:** - `a`, `b`: integers (may be negative, zero, or positive). **Output:** - An integer equal to `|a - b|`. **Constraints:** - `-10^9 <= a, b <= 10^9` (fits within standard 32-bit/64-bit integers).

Constraints

Input integers are between -10^9 and 10^9 inclusive. The function should run in O(1) time and O(1) space.

Example

>>> absolute_difference(5, 3)
2
>>> absolute_difference(3, 5)
2
>>> absolute_difference(-4, 6)
10
>>> absolute_difference(0, 0)
0
5 points ~5 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use Python's built-in `abs()` function.
Subtract one number from the other, then take the absolute value.
Make sure to handle negative results correctly.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.