Wednesday, December 2, 2020

Measuring 6L water from 4L and 9L buckets

Problem: You are given one 4 liter bucket and one 9 liter bucket. The buckets have no measurement lines on them either. How could you measure exactly 6 liter using only these buckets given you have as much extra water as you need.

Solution: Let's say 4L bucket is Bucket_4 and 9L bucket is Bucket_9. Initial value is [Bucket_4: 0, Bucket_9: 0](empty buckets). We can measure 6 liter by following below steps: 

  1. Fill Bucket_9. [Bucket_4: 0, Bucket_9: 9]
  2. Fill Bucket_4 from Bucket_9. [Bucket_4: 4, Bucket_9: 5]
  3. Empty Bucket_4. [Bucket_4: 0, Bucket_9: 5]
  4. Fill Bucket_4 from Bucket_9. [Bucket_4: 4, Bucket_9: 1]
  5. Empty Bucket_4. [Bucket_4: 0, Bucket_9: 1]
  6. Fill Bucket_4 (remaining 1 L water in Bucket_9) from Bucket_9. [Bucket_4: 1, Bucket_9: 0]
  7. Fill Bucket_9. [Bucket_4: 1, Bucket_9: 9]
  8. Fill Bucket_4 from Bucket_9. Bucket_4 can only take 3L more as 1L is already there in Bucket_4. [Bucket_4 : 4, Bucket_9: 6]
Now you can see that at step 8 Bucket_9 will have 6L (9 - 3) water.

No comments:

Post a Comment