SHOM - Editorial

PROBLEM LINK:

Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4

Author: iceknight1093
Tester: sushil2006
Editorialist: iceknight1093

DIFFICULTY:

Cakewalk

PREREQUISITES:

None

PROBLEM:

You have L left shoes and R right shoes.
Each left shoe can be paired with one right shoe.
What’s the minimum number of unpaired shoes you can be left with?

EXPLANATION:

Each left shoe is to be paired with one right shoe.
So,

  • If L \leq R, every left shoe can be paired up.
    This will leave R-L unpaired right shoes.
  • If L \geq R, the opposite is true: every right shoe can be paired up.
    This will leave L-R unpaired left shoes.

Thus, the answer is either R-L or L-R, depending on whether L \leq R or not.
A simpler formulation is to note that in either case, the answer is just the difference between L and R, i.e.

|L-R|

In most languages, this translates to abs(L-R).

TIME COMPLEXITY:

\mathcal{O}(1) per testcase.

CODE:

Editorialist's code (PyPy3)
l, r = map(int, input().split())
print(abs(l-r))