CAKEBAKE7 - Editorial

PROBLEM LINK:

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

Author: raysh07
Tester: sushil2006
Editorialist: iceknight1093

DIFFICULTY:

TBD

PREREQUISITES:

None

PROBLEM:

Chef baked M cakes, and N customers will visit.
Each customer must buy at least one cake.
A customer will be happy if he can buy at least two cakes.
Find the maximum possible number of happy customers.

EXPLANATION

Each customer must buy at least one cake.
So, let’s give one cake to every customer to start with.
This uses up N cakes, meaning there are (M - N) cakes remaining.

Now,

  • If M - N \ge N, then we have enough cakes to give one more cake to each customer.
    This means each customer has at least two cakes, so everyone is happy.
    The answer here is hence N.
  • If M-N \lt N, then we have enough cakes to give one more cake only to (M-N) more customers.
    The answer here is hence M-N.

The above cases can be summarizes as: the answer is either N or (M-N), whichever is smaller.

TIME COMPLEXITY:

\mathcal{O}(1) per testcase.

CODE:

Editorialist's code (PyPy3)
n, m = map(int, input().split())
print(min(n, m-n))