Given two integers N and T denoting the number of levels and the number of seconds respectively, the task is to find the number of completely filled vessels after T seconds under given conditions:
- A structure of vessels of N levels is such that the number of the vessels at each level is equal to the level number i.e 1, 2, 3, … up to N.
- Each vessel can store a maximum of 1 unit of water and in every second 1 unit water is poured out from a tap at a constant rate.
- If the vessel becomes full, then water starts flowing out of it, and pours over the edges of the vessel and is equally distributed over the two connected vessels immediately below it.
- All the objects are arranged symmetrically along the horizontal axis.
- All levels are equally spaced.
- Water flows symmetrically over both the edges of the vessel.
Input: N = 3, T = 2
View of Structure with N = 3 and at a time T = 2 after the tap has been opened
Input: N = 3, T = 4
View of Structure with N = 3 and at a time T = 4 after the tap has been opened
Naive Approach: The simplest approach to solve the problem is to check if it is possible to completely fill x vessels in T seconds or not. If found to be true, check for x+1 vessels and repeat so on to obtain the maximum value of x.
Time Complexity: O(N3)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized using Dynamic Programming. Follow the steps below to solve the problem:
- Store the vessel structure in a Matrix, say M, where M[i][j] denotes the jth vessel in the ith level.
- For any vessel M[i][j], the connected vessels at an immediately lower level are M[i + 1][j] and M[i + 1][j + 1].
- Initially, put all water in the first vessel i, e. M = t.
- Recalculate the state of the matrix at every increment of unit time, starting from the topmost vessel i, e. M = t.
- If the amount of water exceeds the volume of the vessel, the amount flowing down from a vessel is split into 2 equal parts filling the two connected vessels at immediately lower level.
- Measure one litre using two vessels and infinite water supply
- Program to check if tank will overflow, underflow or filled in given time
- Number of containers that can be filled in the given time
- Check if a given circle lies completely inside the ring formed by two concentric circles
- Find if a 2-D array is completely traversed or not by following the cell values
- Product of all sorted subsets of size K using elements whose index divide K completely
- Maximize count of empty water bottles from N filled bottles
- Count 1s in binary matrix having remaining indices of its row and column filled with 0s
- Maximum items that can be filled in K Knapsacks of given Capacity
- Kth Smallest Element of a Matrix of given dimensions filled with product of indices
- Find the time which is palindromic and comes after the given time
- Find all rectangles filled with 0
- 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
- Number of ways of cutting a Matrix such that atleast one cell is filled in each part
- Longest subsequence possible that starts and ends with 1 and filled with 0 in the middle
- Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts
- Minimum time to return array to its original state after given modifications
- Find the minimum time after which one can exchange notes
- Count subarrays such that remainder after dividing sum of elements by K gives count of elements
Below is the implementation of the above approach:
Time Complexity: O(N2)
Auxiliary Space: O(N2)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
Improved By : sanjoy_62