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 = 5
Output: 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 = 8
Output: 6
Approach: This problem is solved using Greedy Approach. Below are the steps:
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:
Below is the implementation of the above approach: