Given a number **N** and a time **X** unit, the task is to find the number of containers that are filled completely in X unit if containers are arranged in pyramid fashion as shown below.

**Note:** Below example is the pyramid arrangement for N = 3, where **N** denotes the number of levels in the pyramid-shaped arrangement of containers such that **level 1 has 1** container, **level 2 has 2 containers** and up to **level N**. The liquid is always poured in the topmost container at the 1st level. When the container of one level overflows from both its sides, the containers of the lower levels are filled. The amount of liquid poured in each second is equal to the volume of the container.

**Examples:**

Input:N = 3, X = 5Output:4

After 1 second, the container at level 1 gets fully filled.

After 2 seconds, the 2 containers at level 2 are half-filled.

After 3 seconds, the 2 containers at level 2 are fully filled.

After 4 seconds, out of the 3 containers at level 3, the 2 containers at the ends are quarter-filled and the container at the center is half-filled.

After 5 seconds, out of the 3 containers at level 3, the 2 containers at the ends are half-filled and the container at the center is fully filled.

Input:N = 4, X = 8Output:6

**Approach:** This problem is solved using Greedy Approach. Below are the steps:

- After the container is getting filled, the liquid oveflows equally from both the sides of the container and fills the containers below it.
- Consider this pyramind of container as a matrix. So,
**cont[i][j]**is the liquid in**j**container of^{th}**i**row.^{th} - So, when overflow occurs, the liquid flows to
**cont[i+1][j]**and**cont[i+1][j+1]**. - As the liquid will be poured for
**X**seconds, so the total amount of liquid poured is**X**units. - So the maximum value that can be poured into a container can be
**X**units and minimum value that can be poured to completely fill it is**1**unit.

As the liquid is poured into the topmost container always, let the topmost container has a maximum value i.e. X units.

Steps for the algorithm are as follows:

- Start the outer loop from
**1 to N**for**n**containers at each level. Inside this loop, start another loop for**1 to i**, for each container of each row. Also declare a counter,**count = 0**, for counting the containers which are getting filled. - If value of
**cont[i][j]**is greater than or equal to**1**(it means it is filled) then, count is incremented by**1**, and then in**cont[i+1][j]**&**g[i+1][j+1]**the liquid gets poured and their value is increased by value of**(g[i][j]-1)/2**respectively, because the liquid is divided in half amount in both of them. - In this way continue the loop, and incrementing the countfor each filled container. When the loop ends our count will be the required answer.

Below is the implementation of the above approach:

## C++

`// C++ program for the above problem ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// matrix of containers ` `double` `cont[1000][1000]; ` ` ` `// function to find the number ` `// of containers that will be ` `// filled in X seconds ` `void` `num_of_containers(` `int` `n, ` ` ` `double` `x) ` `{ ` ` ` `int` `count = 0; ` ` ` ` ` `// container on top level ` ` ` `cont[1][1] = x; ` ` ` `for` `(` `int` `i = 1; i <= n; i++) { ` ` ` `for` `(` `int` `j = 1; j <= i; j++) { ` ` ` ` ` `// if container gets filled ` ` ` `if` `(cont[i][j] >= (` `double` `)1) { ` ` ` `count++; ` ` ` ` ` `// dividing the liquid ` ` ` `// equally in two halves ` ` ` `cont[i + 1][j] ` ` ` `+= (cont[i][j] ` ` ` `- (` `double` `)1) ` ` ` `/ (` `double` `)2; ` ` ` ` ` `cont[i + 1][j + 1] ` ` ` `+= (cont[i][j] ` ` ` `- (` `double` `)1) ` ` ` `/ (` `double` `)2; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `cout << count; ` `} ` ` ` `// driver code ` `int` `main() ` `{ ` ` ` `int` `n = 3; ` ` ` `double` `x = 5; ` ` ` `num_of_containers(n, x); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above problem ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Matrix of containers ` `static` `double` `cont[][] = ` `new` `double` `[` `1000` `][` `1000` `]; ` ` ` `// Function to find the number ` `// of containers that will be ` `// filled in X seconds ` `static` `void` `num_of_containers(` `int` `n, ` `double` `x) ` `{ ` ` ` `int` `count = ` `0` `; ` ` ` ` ` `// Container on top level ` ` ` `cont[` `1` `][` `1` `] = x; ` ` ` `for` `(` `int` `i = ` `1` `; i <= n; i++) ` ` ` `{ ` ` ` `for` `(` `int` `j = ` `1` `; j <= i; j++) ` ` ` `{ ` ` ` ` ` `// If container gets filled ` ` ` `if` `(cont[i][j] >= (` `double` `)` `1` `) ` ` ` `{ ` ` ` `count++; ` ` ` ` ` `// Dividing the liquid ` ` ` `// equally in two halves ` ` ` `cont[i + ` `1` `][j] += (cont[i][j] - ` ` ` `(` `double` `)` `1` `) / ` ` ` `(` `double` `)` `2` `; ` ` ` ` ` `cont[i + ` `1` `][j + ` `1` `] += (cont[i][j] - ` ` ` `(` `double` `)` `1` `) / ` ` ` `(` `double` `)` `2` `; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `System.out.print(count); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `3` `; ` ` ` `double` `x = ` `5` `; ` ` ` ` ` `num_of_containers(n, x); ` `} ` `} ` ` ` `// This code is contributed by jrishabh99 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above problem ` ` ` `# Matrix of containers ` `cont ` `=` `[[ ` `0` `for` `i ` `in` `range` `(` `1000` `)] ` ` ` `for` `j ` `in` `range` `(` `1000` `)] ` ` ` `# Function to find the number ` `# of containers that will be ` `# filled in X seconds ` `def` `num_of_containers(n, x): ` ` ` ` ` `count ` `=` `0` ` ` ` ` `# Container on top level ` ` ` `cont[` `1` `][` `1` `] ` `=` `x ` ` ` ` ` `for` `i ` `in` `range` `(` `1` `, n ` `+` `1` `): ` ` ` `for` `j ` `in` `range` `(` `1` `, i ` `+` `1` `): ` ` ` ` ` `# If container gets filled ` ` ` `if` `(cont[i][j] >` `=` `1` `): ` ` ` `count ` `+` `=` `1` ` ` ` ` `# Dividing the liquid ` ` ` `# equally in two halves ` ` ` `cont[i ` `+` `1` `][j] ` `+` `=` `(cont[i][j] ` `-` `1` `) ` `/` `2` ` ` `cont[i ` `+` `1` `][j ` `+` `1` `] ` `+` `=` `(cont[i][j] ` `-` `1` `) ` `/` `2` ` ` ` ` `print` `(count) ` ` ` `# Driver code ` `n ` `=` `3` `x ` `=` `5` ` ` `num_of_containers(n, x) ` ` ` `# This code is contributed by yatinagg ` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above problem ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Matrix of containers ` `static` `double` `[,]cont = ` `new` `double` `[1000, 1000]; ` ` ` `// Function to find the number ` `// of containers that will be ` `// filled in X seconds ` `static` `void` `num_of_containers(` `int` `n, ` `double` `x) ` `{ ` ` ` `int` `count = 0; ` ` ` ` ` `// Container on top level ` ` ` `cont[1, 1] = x; ` ` ` `for` `(` `int` `i = 1; i <= n; i++) ` ` ` `{ ` ` ` `for` `(` `int` `j = 1; j <= i; j++) ` ` ` `{ ` ` ` ` ` `// If container gets filled ` ` ` `if` `(cont[i, j] >= (` `double` `)1) ` ` ` `{ ` ` ` `count++; ` ` ` ` ` `// Dividing the liquid ` ` ` `// equally in two halves ` ` ` `cont[i + 1, j] += (cont[i, j] - ` ` ` `(` `double` `)1) / ` ` ` `(` `double` `)2; ` ` ` ` ` `cont[i + 1, j + 1] += (cont[i, j] - ` ` ` `(` `double` `)1) / ` ` ` `(` `double` `)2; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `Console.Write(count); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `n = 3; ` ` ` `double` `x = 5; ` ` ` ` ` `num_of_containers(n, x); ` `} ` `} ` ` ` `// This code is contributed by Princi Singh ` |

*chevron_right*

*filter_none*

**Output**

4

**Time Complexity:** O(N^{2}) **Auxiliary space Complexity:** O(N^{2})

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.

## Recommended Posts:

- Program to check if tank will overflow, underflow or filled in given time
- Count of vessels completely filled after a given time
- Kth Smallest Element of a Matrix of given dimensions filled with product of indices
- Maximizing Probability of one type from N containers
- Find the element at the specified index of a Spirally Filled Matrix
- Sum of elements in range L-R where first half and second half is filled with odd and even numbers
- Maximize count of empty water bottles from N filled bottles
- Check if the given array can be reduced to zeros with the given operation performed given number of times
- Maximum time in HH:MM:SS format that can be represented by given six digits
- Check if a given number can be represented in given a no. of digits in any base
- Number of K's such that the given array can be divided into two sets satisfying the given conditions
- Check if given array can be made 0 with given operations performed any number of times
- Minimum time to reach a point with +t and -t moves at time t
- Find maximum number that can be formed using digits of a given number
- Maximum possible time that can be formed from four digits
- Find Index of given fibonacci number in constant time
- Print all combinations of points that can compose a given number
- Check if given number can be represented as sum of two great numbers
- Number of triangles that can be formed with given N points
- Maximum number of teams that can be formed with given persons

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.