FLIPCARDS - Editorial

PROBLEM LINK:

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

Author: Utkarsh Gupta
Tester: Hriday
Editorialist: Nishank Suresh

DIFFICULTY:

641

PREREQUISITES:

None

PROBLEM:

There are N cards, out of which X are face-down. How many cards do you need to flip so that every card faces the same direction?

EXPLANATION:

To make every card face-down, we need to flip each of the N-X cards that are currently face-up.
To make every card face-up, we need to flip each of the X cards that are currently face-down.

Making every card face the same way thus needs the minimum of these two values, i.e, \min(X, N-X) flips.

TIME COMPLEXITY

\mathcal{O}(1) per test case.

CODE:

Editorialist's code (Python)
for _ in range(int(input())):
    n, k = map(int, input().split())
    print(min(k, n-k))