Pour Water Between Buckets to Make Water Levels Equal - Problem

You have n buckets each containing some gallons of water in it, represented by a 0-indexed integer array buckets, where the ith bucket contains buckets[i] gallons of water.

You are also given an integer loss. You want to make the amount of water in each bucket equal. You can pour any amount of water from one bucket to another bucket (not necessarily an integer).

However, every time you pour k gallons of water, you spill loss percent of k. Return the maximum amount of water in each bucket after making the amount of water equal.

Answers within 10-5 of the actual answer will be accepted.

Input & Output

Example 1 — Basic Case
$ Input: buckets = [1,2,7], loss = 80
Output: 2.00000
💡 Note: Pour 5 gallons from bucket 2 to bucket 0. 80% is lost, so bucket 0 gets 1 gallon (total 2), bucket 2 has 2 gallons left. All buckets now have 2 gallons.
Example 2 — High Loss Rate
$ Input: buckets = [2,4,5], loss = 100
Output: 2.00000
💡 Note: With 100% loss, no water can be transferred. The maximum equal level is the minimum value: 2.
Example 3 — No Loss Case
$ Input: buckets = [3,1,3,4,5], loss = 0
Output: 3.20000
💡 Note: With 0% loss, we can perfectly redistribute water. Total water is 16, so each bucket gets 16/5 = 3.2 gallons.

Constraints

  • 1 ≤ buckets.length ≤ 103
  • 0 ≤ buckets[i] ≤ 105
  • 0 ≤ loss ≤ 99

Visualization

Tap to expand
Pour Water Between Buckets to Make Levels EqualInitial State127Pour 5Lose 80%Get 1Final State222All buckets reach equal level: 2.0 gallonsLoss = 80%, so only 20% of transferred water reaches destination
Understanding the Visualization
1
Input
Buckets with different water levels [1,2,7], 80% loss rate
2
Process
Pour water from high buckets to low buckets, losing 80% in transit
3
Output
All buckets reach equal level: 2.0 gallons each
Key Takeaway
🎯 Key Insight: Binary search on the maximum achievable water level, simulating transfers with loss to test feasibility
Asked in
Google 15 Microsoft 8
8.2K Views
Medium Frequency
~35 min Avg. Time
245 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