Pour Water Between Buckets to Make Water Levels Equal - Problem

You have n buckets arranged in a row, each containing a different amount of water measured in gallons. Your goal is to equalize the water levels across all buckets by pouring water from one bucket to another.

However, there's a catch! Every time you pour k gallons of water from one bucket to another, you lose a certain percentage of that water due to spillage. If the loss percentage is loss, then only k Γ— (100 - loss) / 100 gallons actually reach the destination bucket.

Given an array buckets where buckets[i] represents the initial water amount in the i-th bucket, and an integer loss representing the spillage percentage, find the maximum possible water level that can be achieved in each bucket when all buckets have equal amounts.

Note: You can pour any fractional amount of water, and answers within 10-5 of the actual answer will be accepted.

Input & Output

example_1.py β€” Basic Case
$ Input: buckets = [1, 2, 7], loss = 80
β€Ί Output: 2.00000
πŸ’‘ Note: We can pour 5 gallons from bucket 2 to bucket 0 (4 gallons lost, 1 gallon reaches). Final: [2, 2, 2]. Each bucket has 2 gallons.
example_2.py β€” No Transfer Needed
$ Input: buckets = [2, 4, 5], loss = 50
β€Ί Output: 3.66667
πŸ’‘ Note: Pour 1.33 from bucket 2 to bucket 0 (0.67 reaches), and 0.33 from bucket 1 to bucket 0 (0.17 reaches). We can achieve approximately 3.67 gallons in each bucket.
example_3.py β€” High Loss Rate
$ Input: buckets = [100], loss = 0
β€Ί Output: 100.00000
πŸ’‘ Note: Only one bucket, no transfer needed. The water level remains 100 gallons.

Constraints

  • 1 ≀ n ≀ 103
  • 0 ≀ buckets[i] ≀ 105
  • 0 ≀ loss < 100
  • Answers within 10-5 of the actual answer will be accepted

Visualization

Tap to expand
Target Level: 200B1: 150B2: 250B3: 200B4: 180Need: +50Excess: -50Exact: 0Need: +20Feasibility CheckExcess: 50 gallonsDeficit: 70 gallonsLoss: 20%Available: 50Γ—0.8 = 4040 < 70: NOT FEASIBLEBinary Search: Testing Water Level 200
Understanding the Visualization
1
Initial Setup
Start with buckets containing different water amounts
2
Binary Search Bounds
Set left=0, right=max_bucket_value
3
Test Mid Level
Check if we can achieve the middle value in all buckets
4
Calculate Excess/Deficit
Sum excess water above level and deficit below level
5
Account for Loss
Apply loss percentage: can excessΓ—(1-loss%) fill deficit?
6
Update Bounds
Narrow search space based on feasibility
7
Converge to Answer
Continue until finding the maximum achievable level
Key Takeaway
🎯 Key Insight: Binary search efficiently finds the maximum achievable water level by testing feasibility through excess/deficit calculation with loss consideration.
Asked in
Google 25 Amazon 18 Microsoft 15 Meta 12
23.8K Views
Medium Frequency
~25 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen