2. Presenter
• Amrinder Arora
• Adjunct Faculty at GWU, since Spring 2010
• Typically teaching Algorithms
• amrinder@gwu.edu
• On the web at:
• http://www.standardwisdom.com/
• https://www.linkedin.com/in/amrinderarora
• http://www.slideshare.net/amrinderarora
Algorithmic Puzzles 2
3. Puzzles!
• Coins – Real vs. Counterfeit
• Uneven Pitchers
• Strong Eggs and Tiny Floors
• N people in a Circle
Even More..
• Puzzle the Puzzler!
Algorithmic Puzzles 3
4. Find the counterfeit coin.
Do NOT contact the police.
We will be in touch.
Algorithmic Puzzles 4
5. Of 12 coins, one is
counterfeit and weighs
either more or less than
the other coins.
You have an old-fashioned
balance with
two scales.
You have to find out
which coin is counterfeit,
and whether it is lighter
or heavier then the other
coins.
Algorithmic Puzzles 5
6. Counterfeit Coin
• For 12 coins, 3 weighings are sufficient.
• Measure 4 against 4
• If they are equal, compare 3 good coins with 3 unknown ones
• If they are not equal, compare w1 w2 l1 with w3 w4 n1
• If they are equal, either l2, l3 or l4 is lighter.
• If left is heavy, then either w1 or w2 is heavy
• If left is light, either l1 is light, or w3 or w4 is heavy
• For 14 coins, 3 weighings are not sufficient? (Why? Because
log328 > 3).
• How about for 13 coins? (We observe that log326 < 3)
Algorithmic Puzzles 6
7. Counterfeit Coin
(Algorithmic and Decision Tree View)
• 24 possible outcomes overall. Compare 4 vs. 4.
• 8 possible outcomes if Left < Right
• 8 possible outcomes if Right > Left
• 8 possible outcomes if Left = Right
• If we measure any other way (5 vs. 5)
• If 5 vs. 5 then there are 10 possible outcomes if Left < Right
• Log310 > 2 and we cannot solve that particular scenario in 2 more weighings.
• If 3 vs. 3, there are 12 possible outcomes if Left = Right
• Therefore 4 vs. 4 is the only winning strategy.
• For this reason, 13 coins are not possible to solve using only 3 weighings,
even though 26 is less than 3^3.
• If you design a decision tree for n coins, you can use this lower bound
analysis technique to design the right decision tree
Algorithmic Puzzles 7
8. Water and Pitchers
You have two pitchers, one of 5
gallons, and other of 8 gallons
[The pitchers are irregularly
shaped and without markings]
You also have a faucet, and as
much water as you'd like.
Can you get 3 gallons?
Can you obtain 1 gallon? 2? 4?
6? 7?
Algorithmic Puzzles 8
9. Water and Pitchers (cont.)
Where can we go from here:
(x,y)
a. (0,y) / (x,0) Empty first/second
b. (5,y) / (x,8) Fill first/second
c. (5,x+y-5) Second to First, x+y > 5
d. (x+y,0) Second to First, x+y ≤ 5
e. (x+y-8,8) First to Second, x+y > 8
f. (0,x+y) First to Second, x+y ≤ 8
Algorithmic Puzzles 9
10. Water and Pitchers (cont.)
(0,0)
1. (0,8) // b. fill second
2. (5,3) // c. second to first
3. (0,3) // a. empty first
4. (3,0) // d. second to first
5. (3,8) // b. fill second
6. (5,6) // c. second to first
7. (0,6) // a. empty first
8. (5,1) // c. second to first
9. (0,1) // a. empty first
Algorithmic Puzzles 10
11. Water and Pitchers (cont.)
• Variation. We are also given an infinite pitcher.
• So, given 71 and 74 gallon pitchers, we should be able to get
67 gallons, or 20167 gallons (possibly in the infinite pitcher)
• Question: What is the minimum number of moves that you
need to collect 1 gallon?
Algorithmic Puzzles 11
12. Water and Pitchers (cont.)
• GCD: greatest common divisor. Largest number that divides
both the given numbers.
• Relevant to this puzzle for multiple reasons.
• GCD is also defined as the smallest positive number that can
be written as a linear combination of given numbers with
integer coefficients.
• GCD(n,m) = n + m, where , are integers.
• GCD can be calculated using Euclid’s algorithm.
http://www.slideshare.net/amrinderarora/euclids-algorithm-
for-greatest-common-divisor
Algorithmic Puzzles 12
13. Water and Pitchers (cont.)
• GCD(74,71) = 1 = 24 x 74 – 25 x 71
Algorithmic Puzzles 13
15. Eggs and Floors
• Given n floors and m eggs
• Need to find the highest floor from which eggs can be
thrown safely.
•We need to minimize the number of throws (not broken
eggs)
• If an egg is thrown and it survives, then it can be reused.
Algorithmic Puzzles 15
16. Eggs and Floors (cont.)
• Comment from “Brandon”
This entire “puzzle” is based on an assumption that an egg really can
survive a 100 story drop. Personally I have never seen an egg fall more
than about 8″ without breaking so this is a theoretical, hypothetical
question with no factual basis. Both eggs could break after the 1st drop
from the 1st floor which means your whole experiment is screwed…
Algorithmic Puzzles 16
17.
18. Strong Eggs and Tiny Floors
[We need to adjust the puzzle.]
• Given n tiny floors and m strong eggs
• Need to find the highest floor from which eggs can be
thrown safely.
•We need to minimize the number of throws (not broken
eggs)
• If an egg is thrown and it survives, then it can be reused.
Algorithmic Puzzles 18
20. Strong Eggs and Tiny Floors (cont.)
• Let f(n,m) be the minimum number of attempts given n
floors and m eggs. [There is no guarantee that we will still
have the egg intact after the test.]
• Then, f(n,1) = n // We have no option but to climb floors
one by one.
• Similarly, f(1,m) = 1 // We just need one try if there is only
one floor
Algorithmic Puzzles 20
21. Strong Eggs and Tiny Floors (cont.)
• The recursion is built around the first action – which floor do we try the
first egg from. If that is j, and if the egg breaks, then we have j-1 floors
left, m-1 eggs left. If the egg doesn’t break, then we have n-j floors and
m eggs left. Recursive formula:
• Dynamic Programming implementation:
• nm entries in the table.
• To compute each entry, we need O(n) time.
• O(n2m) time.
• O(nm) time is also possible – no need to span the full range of j.
• Source code at:
http://standardwisdom.com/softwarejournal/2010/10/puzzles-and-answers/
Algorithmic Puzzles 21
22. People in a Circle Puzzle
• There are n people are standing in a circle, numbered from
1..n
• Every k-th person (for example, second) person sits down
going around the circle repeatedly until there is only one
person standing.
• What position person is the last one standing?
• Examples
• n = 3, k = 2. Person 2 sits down. Person 1 sits down. Person 3 is the
last person standing.
• n = 5, k = 3. Person 3. Person 1. Person 5. Person 2. Person 4 is the
last person standing.
Algorithmic Puzzles 22
23. People in a Circle Puzzle (Cont.)
• Analyzing the specific case of k = 2
• Start with special case of n = 2m. In that case, person 1 is
clearly the last person standing.
•We can now consider the general case that n = 2m + z.
• In the first round, after we have eliminated z players, that
means we are looking at 2z+1 player as our first player, and
now there are 2m players in the game, so by that logic, 2z+1
player should be the last person standing.
Algorithmic Puzzles 23
24. Puzzle the Puzzler!
• Grab a pizza.
• Grab a drink.
• Ask me a question.
• Send it via the contact form at
http://www.standardwisdom.com/
• Or, send it to me at: amrinder@gwu.edu
Algorithmic Puzzles 24
25. Image Credits
• www.lumaxart.com/ for
• http://www.keepcalm-o-matic.co.uk/p/makes-no-sense-at-all/
for “Makes no sense”
• http://www.diylol.com/ for “It all makes sense now”
• Flickr user SamFa - https://www.flickr.com/photos/tromal/
for picture of the pottery.
• http://en.wikipedia.org/wiki/Empire_State_Building#mediav
iewer/File:Looking_Up_at_Empire_State_Building.JPG for
the Empire State Building Picture (GPL).
• http://www.myteespot.com/ “Drop it like its hot” angry bird.
Algorithmic Puzzles 25