There are some glasses with equal capacity as 1 litre. The glasses are kept as follows:
1 2 3 4 5 6 7 8 9 10
You can put water to only top glass. If you put more than 1 litre water to 1st glass, water overflows and fills equally in both 2nd and 3rd glasses. Glass 5 will get water from both 2nd glass and 3rd glass and so on.
If you have X litre of water and you put that water in top glass, how much water will be contained by jth glass in ith row?
Example. If you will put 2 litre on top.
1st – 1 litre
2nd – 1/2 litre
3rd – 1/2 litre
1 ---------------- 1 2 3 ---------------- 2 4 5 6 ------------ 3 7 8 9 10 --------- 4
Each glass contributes to the two glasses down the glass. Initially, we put all water in first glass. Then we keep 1 litre (or less than 1 litre) in it, and move rest of the water to two glasses down to it. We follow the same process for the two glasses and all other glasses till ith row. There will be i*(i+1)/2 glasses till ith row.
Amount of water in jth glass of ith row is: 0.500000
Time Complexity: O(i*(i+1)/2) or O(i^2)
Auxiliary Space: O(i*(i+1)/2) or O(i^2)
This article is compiled by Rahul and reviewed by GeeksforGeeks team. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find amount of water wasted after filling the tank
- Program to check if water tank overflows when n solid balls are dipped in the water tank
- Find if the glass will be empty or not when the rate of drinking is given
- Find amount to be added to achieve target ratio in a given mixture
- Maximum sum of cocktail glass in a 2D matrix
- Find out the minimum number of coins required to pay total amount
- Find the maximum angle at which we can tilt the bottle without spilling any water
- Measure one litre using two vessels and infinite water supply
- Trapping Rain Water
- Container with Most Water
- Water Jug Problem using Memoization
- Minimum sprinklers required to water a rectangular park
- Maximize count of empty water bottles from N filled bottles
- Program to find sum of elements in a given array
- Program to find sum of the given sequence
- Program to find the count of coins of each type from the given ratio
- Program to find count of numbers having odd number of divisors in given range
- Program to find the number from given holes
- Program to find absolute value of a given number
- C program to find square root of a given number