Skip to content

East-West Highway

Written by Iyed Baassou

Problem Statement

English
Arabic
French

Solution

By definition, the distance from to is denoted as , and it's called the absolute value of . This can be applied to the distance from to as .


Solution 1:

Fortunately, most programming languages like C++ and python provide a built-in function to calculate absolute value called abs() which makes the task way easier.

Note: the following solutions are just for the fun of the reader and are not a good practice when you have an easy solution like Solution 1


Solution 2:

We can also solve this without abs() using the definition of absolute value:

Applying it to :


Solution 3:

Another cool way to solve it is observing that

Which translates to

Implementation

For sake of simplicity, we will be implementing solutions in Python.

py
# Solution 1:
a = int(input())
b = int(input())
print(abs(a - b))
py
# Solution 2:
a = int(input())
b = int(input())
if a - b >= 0: print(a - b)
else print(b - a)
py
# Solution 3:
a = int(input())
b = int(input())
print(max(a - b, b - a))