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.
Assumptions:
- 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.
Examples:
Input: N = 3, T = 2
Output: 1
Explanation:
View of Structure with N = 3 and at a time T = 2 after the tap has been opened
Input: N = 3, T = 4
Output: 3
Explanation:
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[0][0] = t.
- Recalculate the state of the matrix at every increment of unit time, starting from the topmost vessel i, e. M[0][0] = 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
- Maximize count of empty water bottles from N filled bottles
- 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
- Find the time which is palindromic and comes after the given time
- 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 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
- Longest subsequence possible that starts and ends with 1 and filled with 0 in the middle
- Number of ways of cutting a Matrix such that atleast one cell is filled in each part
- 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
- Count subarrays such that remainder after dividing sum of elements by K gives count of elements
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Find the minimum time after which one can exchange notes
Below is the implementation of the above approach:
C++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; int n, t; // Function to find the number of // completely filled vessels int FindNoOfFullVessels(int n, int t) { // Store the vessels double Matrix[n][n]; // Assuming all water is present // in the vessel at the first level Matrix[0][0] = t * 1.0; // Store the number of vessel // that are completely full int ans = 0; // Traverse all the levels for(int i = 0; i < n; i++) { // Number of vessel at each // level is j for(int j = 0; j <= i; j++) { // Calculate the exceeded // amount of water double exceededwater = Matrix[i][j] - 1.0; // If current vessel has // less than 1 unit of // water then continue if (exceededwater < 0) continue; // One more vessel is full ans++; // If left bottom vessel present if (i + 1 < n) Matrix[i + 1][j] += exceededwater / 2; // If right bottom vessel present if (i + 1 < n && j + 1 < n) Matrix[i + 1][j + 1] += exceededwater / 2; } } return ans; } // Driver Code int main() { // Number of levels int N = 3; // Number of seconds int T = 4; // Function call cout << FindNoOfFullVessels(N, T) << endl; return 0; } // This code is contributed by sanjoy_62 |
Java
// Java Program to implement // the above approach import java.io.*; import java.util.*; class GFG { static int n, t; // Function to find the number of // completely filled vessels public static int FindNoOfFullVessels(int n, int t) { // Store the vessels double Matrix[][] = new double[n][n]; // Assuming all water is present // in the vessel at the first level Matrix[0][0] = t * 1.0; // Store the number of vessel // that are completely full int ans = 0; // Traverse all the levels for (int i = 0; i < n; i++) { // Number of vessel at each // level is j for (int j = 0; j <= i; j++) { // Calculate the exceeded // amount of water double exceededwater = Matrix[i][j] - 1.0; // If current vessel has // less than 1 unit of // water then continue if (exceededwater < 0) continue; // One more vessel is full ans++; // If left bottom vessel present if (i + 1 < n) Matrix[i + 1][j] += exceededwater / 2; // If right bottom vessel present if (i + 1 < n && j + 1 < n) Matrix[i + 1][j + 1] += exceededwater / 2; } } return ans; } // Driver Code public static void main(String[] args) { // Number of levels int N = 3; // Number of seconds int T = 4; // Function call System.out.println( FindNoOfFullVessels(N, T)); } } |
C#
// C# program to implement // the above approach using System; class GFG{ //static int n, t; // Function to find the number of // completely filled vessels public static int FindNoOfFullVessels(int n, int t) { // Store the vessels double[,] Matrix = new double[n, n]; // Assuming all water is present // in the vessel at the first level Matrix[0, 0] = t * 1.0; // Store the number of vessel // that are completely full int ans = 0; // Traverse all the levels for(int i = 0; i < n; i++) { // Number of vessel at each // level is j for(int j = 0; j <= i; j++) { // Calculate the exceeded // amount of water double exceededwater = Matrix[i, j] - 1.0; // If current vessel has // less than 1 unit of // water then continue if (exceededwater < 0) continue; // One more vessel is full ans++; // If left bottom vessel present if (i + 1 < n) Matrix[i + 1, j] += exceededwater / 2; // If right bottom vessel present if (i + 1 < n && j + 1 < n) Matrix[i + 1, j + 1] += exceededwater / 2; } } return ans; } // Driver Code public static void Main() { // Number of levels int N = 3; // Number of seconds int T = 4; // Function call Console.WriteLine(FindNoOfFullVessels(N, T)); } } // This code is contributed by sanjoy_62 |
3
Time Complexity: O(N2)
Auxiliary Space: O(N2)
Recommended Posts:
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.
Improved By : sanjoy_62




