PROBLEM LINK:
Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4
Author: raysh07
Tester: iceknight1093
Editorialist: iceknight1093
DIFFICULTY:
Cakewalk
PREREQUISITES:
None
PROBLEM:
A bus has N rows of seats, with two seats in each row.
K people enter the bus.
Each person tries to sit in an empty row; and only if they can’t find one, will sit next to somebody else.
Find the number of people who will sit next to someone else.
EXPLANATION:
If K \le N, then one person each can sit in row 1, 2, 3, \ldots, K, so that nobody is sitting next to anybody else.
The answer in this case is 0.
If K \gt N, then some people are forced to sit next to each other.
In particular, the first N people will each occupy one new row.
This leaves K - N people who are forced to sit next to someone else.
Thus, K-N rows will contain two people, and the answer is
TIME COMPLEXITY:
\mathcal{O}(1) per testcase.
CODE:
Editorialist's code (PyPy3)
for _ in range(int(input())):
n, k = map(int, input().split())
print(max(0, 2*(k - n)))