← All problemsFree · Python · 60 min

Algorithm design / guarantees under an adversary

The Lightest and the Heaviest

Find the lightest and heaviest of n parcels with a balance scale in as few weighings as possible, then write the adversary that proves no method can do better.

0 / 3 graded parts passSaved in this browser · loading saved work…
Jump to your work ↓

A courier depot has n parcels, numbered 0 to n−1, and a balance scale. One weighing puts two parcels on the scale and shows which is lighter. No two parcels weigh the same. The depot needs the lightest parcel and the heaviest parcel, and every weighing takes time.

after weighing 0 vs 1 and 2 vs 30 vs 1: parcel 1 is lighter2 vs 3: parcel 2 is lighterparcel 0lostwonparcel 1lostwonparcel 2lostwonparcel 3lostwonparcel 4lostwonparcel 5lostwonlightest: the one parcel that never loses · heaviest: the one that never wins
Six parcels after two weighings: the ledger of who has won and who has lost.

In code, a weighing is a call lighter(i, j), which returns True when parcel i is lighter than parcel j.

Finding the heaviest alone takes n−1 weighings: keep the heavier parcel of each weighing and bring on the next one. Then finding the lightest among the other n−1 parcels takes n−2 more, 2n−3 in all. Can you do better? And how would you prove that nobody can do better than you?

Grading

  • Your method is weighed against an adversary. The adversary decides the weights as you go, so that your weighings tell you as little as possible. It is also weighed against several fixed orders, for every n from 1 to 40. Your answer must be proved by your weighings: if they leave any other parcel able to be lighter (or heavier), the answer is wrong, even if you guessed right.
  • Your adversary is played against several weighing strategies, including ones that bet on a particular order. Its answers must fit some ordering of the parcels.
  • No timing decides a grade.

Background: Lower Bounds and Adversaries.

easy · Part A

Six parcels

For 6 parcels, what is the fewest number of weighings that always suffices to find both the lightest and the heaviest, however the weights turn out?

Explore in the sandbox

Experiment here, then copy a rule into the editor and test its guarantees.