SlideShare a Scribd company logo
1 of 25
Algorithmic 
Puzzles
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
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
Find the counterfeit coin. 
Do NOT contact the police. 
We will be in touch. 
Algorithmic Puzzles 4
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
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
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
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
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
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
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
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
Water and Pitchers (cont.) 
• GCD(74,71) = 1 = 24 x 74 – 25 x 71 
Algorithmic Puzzles 13
Strong Eggs and 
Tiny Floors 
Algorithmic Puzzles 14
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
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
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
Algorithmic Puzzles 19
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
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
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
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
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
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

More Related Content

Viewers also liked

1.6 slopes and the difference quotient
1.6 slopes and the difference quotient1.6 slopes and the difference quotient
1.6 slopes and the difference quotient
math265
 

Viewers also liked (20)

Dynamic Programming - Part 1
Dynamic Programming - Part 1Dynamic Programming - Part 1
Dynamic Programming - Part 1
 
Online Algorithms - An Introduction
Online Algorithms - An IntroductionOnline Algorithms - An Introduction
Online Algorithms - An Introduction
 
Binomial Heaps and Fibonacci Heaps
Binomial Heaps and Fibonacci HeapsBinomial Heaps and Fibonacci Heaps
Binomial Heaps and Fibonacci Heaps
 
Splay Trees and Self Organizing Data Structures
Splay Trees and Self Organizing Data StructuresSplay Trees and Self Organizing Data Structures
Splay Trees and Self Organizing Data Structures
 
NP-Completeness - II
NP-Completeness - IINP-Completeness - II
NP-Completeness - II
 
Tries - Tree Based Structures for Strings
Tries - Tree Based Structures for StringsTries - Tree Based Structures for Strings
Tries - Tree Based Structures for Strings
 
Graph Traversal Algorithms - Breadth First Search
Graph Traversal Algorithms - Breadth First SearchGraph Traversal Algorithms - Breadth First Search
Graph Traversal Algorithms - Breadth First Search
 
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of PointsDivide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
 
Online algorithms in Machine Learning
Online algorithms in Machine LearningOnline algorithms in Machine Learning
Online algorithms in Machine Learning
 
1.6 slopes and the difference quotient
1.6 slopes and the difference quotient1.6 slopes and the difference quotient
1.6 slopes and the difference quotient
 
Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...
Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...
Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...
 
Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)
Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)
Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)
 
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
 
Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...
Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...
Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...
 
Operational research
Operational researchOperational research
Operational research
 
Graphs, Trees, Paths and Their Representations
Graphs, Trees, Paths and Their RepresentationsGraphs, Trees, Paths and Their Representations
Graphs, Trees, Paths and Their Representations
 
Time complexity of union find
Time complexity of union findTime complexity of union find
Time complexity of union find
 
Lecture22
Lecture22Lecture22
Lecture22
 
Algorithm Design and Complexity - Course 1&2
Algorithm Design and Complexity - Course 1&2Algorithm Design and Complexity - Course 1&2
Algorithm Design and Complexity - Course 1&2
 
Arima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet Mahana
Arima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet MahanaArima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet Mahana
Arima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet Mahana
 

Similar to Algorithmic Puzzles

6. Permutations and Combinations-Revised (1).pptx
6. Permutations and Combinations-Revised (1).pptx6. Permutations and Combinations-Revised (1).pptx
6. Permutations and Combinations-Revised (1).pptx
TonmoyKabiraj
 
Tcs 2011with solutions
Tcs 2011with solutionsTcs 2011with solutions
Tcs 2011with solutions
Ashu0711
 
[Maths] arithmetic
[Maths] arithmetic[Maths] arithmetic
[Maths] arithmetic
Ourutopy
 
Kahn Help on ACT Math 1
Kahn Help on ACT Math 1 Kahn Help on ACT Math 1
Kahn Help on ACT Math 1
Jim Mathews
 

Similar to Algorithmic Puzzles (20)

unit-3-permutation_combination.pptx
unit-3-permutation_combination.pptxunit-3-permutation_combination.pptx
unit-3-permutation_combination.pptx
 
Chapter7ppt.pdf
Chapter7ppt.pdfChapter7ppt.pdf
Chapter7ppt.pdf
 
6. Permutations and Combinations-Revised (1).pptx
6. Permutations and Combinations-Revised (1).pptx6. Permutations and Combinations-Revised (1).pptx
6. Permutations and Combinations-Revised (1).pptx
 
Maths can be fun
Maths can be funMaths can be fun
Maths can be fun
 
Probability and Statistics - Week 1
Probability and Statistics - Week 1Probability and Statistics - Week 1
Probability and Statistics - Week 1
 
Beginners counting and probability.pptx
Beginners counting and probability.pptxBeginners counting and probability.pptx
Beginners counting and probability.pptx
 
Tcs 2011with solutions
Tcs 2011with solutionsTcs 2011with solutions
Tcs 2011with solutions
 
Counting
CountingCounting
Counting
 
Grade 8 Probability Cambridge [PPT]
Grade 8 Probability Cambridge [PPT]Grade 8 Probability Cambridge [PPT]
Grade 8 Probability Cambridge [PPT]
 
Math Homework Help
Math Homework HelpMath Homework Help
Math Homework Help
 
Math Homework Help
Math Homework HelpMath Homework Help
Math Homework Help
 
Stat.pptx
Stat.pptxStat.pptx
Stat.pptx
 
[Maths] arithmetic
[Maths] arithmetic[Maths] arithmetic
[Maths] arithmetic
 
MOLE CONCEPT.ppt
MOLE CONCEPT.pptMOLE CONCEPT.ppt
MOLE CONCEPT.ppt
 
Gambling standard
Gambling standardGambling standard
Gambling standard
 
Probability Theory for Data Scientists
Probability Theory for Data ScientistsProbability Theory for Data Scientists
Probability Theory for Data Scientists
 
Mathematics high school level quiz - Part I
Mathematics high school level quiz - Part IMathematics high school level quiz - Part I
Mathematics high school level quiz - Part I
 
Kahn Help for ACT Math III
Kahn Help for ACT Math IIIKahn Help for ACT Math III
Kahn Help for ACT Math III
 
Kahn Help on ACT Math 1
Kahn Help on ACT Math 1 Kahn Help on ACT Math 1
Kahn Help on ACT Math 1
 
Lec 06
Lec 06Lec 06
Lec 06
 

More from Amrinder Arora

More from Amrinder Arora (9)

Greedy Algorithms
Greedy AlgorithmsGreedy Algorithms
Greedy Algorithms
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
 
Asymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAsymptotic Notation and Data Structures
Asymptotic Notation and Data Structures
 
Introduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationIntroduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic Notation
 
Set Operations - Union Find and Bloom Filters
Set Operations - Union Find and Bloom FiltersSet Operations - Union Find and Bloom Filters
Set Operations - Union Find and Bloom Filters
 
R-Trees and Geospatial Data Structures
R-Trees and Geospatial Data StructuresR-Trees and Geospatial Data Structures
R-Trees and Geospatial Data Structures
 
Binary Search Trees - AVL and Red Black
Binary Search Trees - AVL and Red BlackBinary Search Trees - AVL and Red Black
Binary Search Trees - AVL and Red Black
 
Stacks, Queues, Binary Search Trees - Lecture 1 - Advanced Data Structures
Stacks, Queues, Binary Search Trees -  Lecture 1 - Advanced Data StructuresStacks, Queues, Binary Search Trees -  Lecture 1 - Advanced Data Structures
Stacks, Queues, Binary Search Trees - Lecture 1 - Advanced Data Structures
 
Learning to learn
Learning to learnLearning to learn
Learning to learn
 

Recently uploaded

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Recently uploaded (20)

presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 

Algorithmic Puzzles

  • 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
  • 14. Strong Eggs and Tiny Floors Algorithmic Puzzles 14
  • 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