Program to find amount of water in a given glass
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
The approach is similar to Method 2 of the Pascal’s Triangle. If we take a closer look at the problem, the problem boils down to Pascal’s Triangle.
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.
C++
// Program to find the amount of water in j-th glass // of i-th row #include <stdio.h> #include <stdlib.h> #include <string.h> // Returns the amount of water in jth glass of ith row float findWater(int i, int j, float X) { // A row number i has maximum i columns. So input // column number must be less than i if (j > i) { printf("Incorrect Inputn"); exit(0); } // There will be i*(i+1)/2 glasses till ith row // (including ith row) float glass[i * (i + 1) / 2]; // Initialize all glasses as empty memset(glass, 0, sizeof(glass)); // Put all water in first glass int index = 0; glass[index] = X; // Now let the water flow to the downward glasses // till the row number is less than or/ equal to i (given row) // correction : X can be zero for side glasses as they have lower rate to fill for (int row = 1; row <= i ; ++row) { // Fill glasses in a given row. Number of // columns in a row is equal to row number for (int col = 1; col <= row; ++col, ++index) { // Get the water from current glass X = glass[index]; // Keep the amount less than or equal to // capacity in current glass glass[index] = (X >= 1.0f) ? 1.0f : X; // Get the remaining amount X = (X >= 1.0f) ? (X - 1) : 0.0f; // Distribute the remaining amount to // the down two glasses glass[index + row] += X / 2; glass[index + row + 1] += X / 2; } } // The index of jth glass in ith row will // be i*(i-1)/2 + j - 1 return glass[i*(i-1)/2 + j - 1]; } // Driver program to test above function int main() { int i = 2, j = 2; float X = 2.0; // Total amount of water printf("Amount of water in jth glass of ith row is: %f", findWater(i, j, X)); return 0; } |
Java
// Program to find the amount /// of water in j-th glass // of i-th row import java.lang.*; class GFG { // Returns the amount of water // in jth glass of ith row static float findWater(int i, int j, float X) { // A row number i has maximum i // columns. So input column // number must be less than i if (j > i) { System.out.println("Incorrect Input"); System.exit(0); } // There will be i*(i+1)/2 glasses // till ith row (including ith row) int ll = Math.round((i * (i + 1) )); float[] glass = new float[ll + 2]; // Put all water in first glass int index = 0; glass[index] = X; // Now let the water flow to the // downward glasses till the row // number is less than or/ equal // to i (given row) // correction : X can be zero for side // glasses as they have lower rate to fill for (int row = 1; row <= i ; ++row) { // Fill glasses in a given row. Number of // columns in a row is equal to row number for (int col = 1; col <= row; ++col, ++index) { // Get the water from current glass X = glass[index]; // Keep the amount less than or // equal to capacity in current glass glass[index] = (X >= 1.0f) ? 1.0f : X; // Get the remaining amount X = (X >= 1.0f) ? (X - 1) : 0.0f; // Distribute the remaining amount // to the down two glasses glass[index + row] += X / 2; glass[index + row + 1] += X / 2; } } // The index of jth glass in ith // row will be i*(i-1)/2 + j - 1 return glass[(int)(i * (i - 1) / 2 + j - 1)]; } // Driver Code public static void main(String[] args) { int i = 2, j = 2; float X = 2.0f; // Total amount of water System.out.println("Amount of water in jth " + "glass of ith row is: " + findWater(i, j, X)); } } // This code is contributed by mits |
Python3
# Program to find the amount # of water in j-th glass of # i-th row # Returns the amount of water # in jth glass of ith row def findWater(i, j, X): # A row number i has maximum # i columns. So input column # number must be less than i if (j > i): print("Incorrect Input"); return; # There will be i*(i+1)/2 # glasses till ith row # (including ith row) # and Initialize all glasses # as empty glass = [0]*int(i *(i + 1) / 2); # Put all water # in first glass index = 0; glass[index] = X; # Now let the water flow to # the downward glasses till # the row number is less # than or/ equal to i (given # row) correction : X can be # zero for side glasses as # they have lower rate to fill for row in range(1,i): # Fill glasses in a given # row. Number of columns # in a row is equal to row number for col in range(1,row+1): # Get the water # from current glass X = glass[index]; # Keep the amount less # than or equal to # capacity in current glass glass[index] = 1.0 if (X >= 1.0) else X; # Get the remaining amount X = (X - 1) if (X >= 1.0) else 0.0; # Distribute the remaining # amount to the down two glasses glass[index + row] += (X / 2); glass[index + row + 1] += (X / 2); index+=1; # The index of jth glass # in ith row will # be i*(i-1)/2 + j - 1 return glass[int(i * (i - 1) /2 + j - 1)]; # Driver Code if __name__ == "__main__": i = 2; j = 2; X = 2.0; # Total amount of water res=repr(findWater(i, j, X)); print("Amount of water in jth glass of ith row is:",res.ljust(8,'0')); # This Code is contributed by mits |
C#
// Program to find the amount // of water in j-th glass // of i-th row using System; class GFG { // Returns the amount of water // in jth glass of ith row static float findWater(int i, int j, float X) { // A row number i has maximum i // columns. So input column // number must be less than i if (j > i) { Console.WriteLine("Incorrect Input"); Environment.Exit(0); } // There will be i*(i+1)/2 glasses // till ith row (including ith row) int ll = (int)Math.Round((double)(i * (i + 1))); float[] glass = new float[ll + 2]; // Put all water in first glass int index = 0; glass[index] = X; // Now let the water flow to the // downward glasses till the row // number is less than or/ equal // to i (given row) // correction : X can be zero // for side glasses as they have // lower rate to fill for (int row = 1; row <= i ; ++row) { // Fill glasses in a given row. // Number of columns in a row // is equal to row number for (int col = 1; col <= row; ++col, ++index) { // Get the water from current glass X = glass[index]; // Keep the amount less than // or equal to capacity in // current glass glass[index] = (X >= 1.0f) ? 1.0f : X; // Get the remaining amount X = (X >= 1.0f) ? (X - 1) : 0.0f; // Distribute the remaining amount // to the down two glasses glass[index + row] += X / 2; glass[index + row + 1] += X / 2; } } // The index of jth glass in ith // row will be i*(i-1)/2 + j - 1 return glass[(int)(i * (i - 1) / 2 + j - 1)]; } // Driver Code static void Main() { int i = 2, j = 2; float X = 2.0f; // Total amount of water Console.WriteLine("Amount of water in jth " + "glass of ith row is: " + findWater(i, j, X)); } } // This code is contributed by mits |
PHP
<?php // Program to find the amount // of water in j-th glass of // i-th row // Returns the amount of water // in jth glass of ith row function findWater($i, $j, $X) { // A row number i has maximum // i columns. So input column // number must be less than i if ($j > $i) { echo "Incorrect Input\n"; return; } // There will be i*(i+1)/2 // glasses till ith row // (including ith row) // and Initialize all glasses // as empty $glass = array_fill(0, (int)($i * ($i + 1) / 2), 0); // Put all water // in first glass $index = 0; $glass[$index] = $X; // Now let the water flow to // the downward glasses till // the row number is less // than or/ equal to i (given // row) correction : X can be // zero for side glasses as // they have lower rate to fill for ($row = 1; $row < $i ; ++$row) { // Fill glasses in a given // row. Number of columns // in a row is equal to row number for ($col = 1; $col <= $row; ++$col, ++$index) { // Get the water // from current glass $X = $glass[$index]; // Keep the amount less // than or equal to // capacity in current glass $glass[$index] = ($X >= 1.0) ? 1.0 : $X; // Get the remaining amount $X = ($X >= 1.0) ? ($X - 1) : 0.0; // Distribute the remaining // amount to the down two glasses $glass[$index + $row] += (double)($X / 2); $glass[$index + $row + 1] += (double)($X / 2); } } // The index of jth glass // in ith row will // be i*(i-1)/2 + j - 1 return $glass[(int)($i * ($i - 1) / 2 + $j - 1)]; } // Driver Code $i = 2; $j = 2; $X = 2.0; // Total amount of water echo "Amount of water in jth " , "glass of ith row is: ". str_pad(findWater($i, $j, $X), 8, '0'); // This Code is contributed by mits ?> |
Output:
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
Recommended Posts:
- 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 amount to be added to achieve target ratio in a given mixture
- Find out the minimum number of coins required to pay total amount
- Maximum sum of cocktail glass in a 2D matrix
- The Two Water Jug Puzzle
- Container with Most Water
- Trapping Rain Water
- Water Jug Problem using Memoization
- Measure one litre using two vessels and infinite water supply
- Program to find sum of 1 + x/2! + x^2/3! +...+x^n/(n+1)!
- Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!
- Program to find Sum of a Series a^1/1! + a^2/2! + a^3/3! + a^4/4! +…….+ a^n/n!
- Program to find the Depreciation of Value
- Program to find sum of the given sequence

