Count of vessels completely filled after a given time
Last Updated :
26 May, 2022
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.
C++
#include <bits/stdc++.h>
using namespace std;
int n, t;
int FindNoOfFullVessels( int n, int t)
{
double Matrix[n][n];
Matrix[0][0] = t * 1.0;
int ans = 0;
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j <= i; j++)
{
double exceededwater = Matrix[i][j] - 1.0;
if (exceededwater < 0)
continue ;
ans++;
if (i + 1 < n)
Matrix[i + 1][j] += exceededwater / 2;
if (i + 1 < n && j + 1 < n)
Matrix[i + 1][j + 1] += exceededwater / 2;
}
}
return ans;
}
int main()
{
int N = 3;
int T = 4;
cout << FindNoOfFullVessels(N, T) << endl;
return 0;
}
|
C
#include <stdio.h>
int n, t;
int FindNoOfFullVessels( int n, int t)
{
double Matrix[n][n];
Matrix[0][0] = t * 1.0;
int ans = 0;
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j <= i; j++)
{
double exceededwater = Matrix[i][j] - 1.0;
if (exceededwater < 0)
continue ;
ans++;
if (i + 1 < n)
Matrix[i + 1][j] += exceededwater / 2;
if (i + 1 < n && j + 1 < n)
Matrix[i + 1][j + 1] += exceededwater / 2;
}
}
return ans;
}
int main()
{
int N = 3;
int T = 4;
printf ( "%d" , FindNoOfFullVessels(N, T));
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int n, t;
public static int
FindNoOfFullVessels( int n, int t)
{
double Matrix[][]
= new double [n][n];
Matrix[ 0 ][ 0 ] = t * 1.0 ;
int ans = 0 ;
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j <= i; j++) {
double exceededwater
= Matrix[i][j] - 1.0 ;
if (exceededwater < 0 )
continue ;
ans++;
if (i + 1 < n)
Matrix[i + 1 ][j]
+= exceededwater / 2 ;
if (i + 1 < n && j + 1 < n)
Matrix[i + 1 ][j + 1 ]
+= exceededwater / 2 ;
}
}
return ans;
}
public static void main(String[] args)
{
int N = 3 ;
int T = 4 ;
System.out.println(
FindNoOfFullVessels(N, T));
}
}
|
Python3
def FindNoOfFullVessels(n, t) :
Matrix = [[ 0 for i in range (n)] for j in range (n)]
Matrix[ 0 ][ 0 ] = t * 1.0
ans = 0
for i in range (n) :
for j in range (i + 1 ) :
exceededwater = Matrix[i][j] - 1.0
if (exceededwater < 0 ) :
continue
ans + = 1
if (i + 1 < n) :
Matrix[i + 1 ][j] + = exceededwater / 2
if (i + 1 < n and j + 1 < n) :
Matrix[i + 1 ][j + 1 ] + = exceededwater / 2
return ans
N = 3
T = 4
print (FindNoOfFullVessels(N, T))
|
C#
using System;
class GFG{
public static int FindNoOfFullVessels( int n,
int t)
{
double [,] Matrix = new double [n, n];
Matrix[0, 0] = t * 1.0;
int ans = 0;
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j <= i; j++)
{
double exceededwater = Matrix[i, j] - 1.0;
if (exceededwater < 0)
continue ;
ans++;
if (i + 1 < n)
Matrix[i + 1, j] += exceededwater / 2;
if (i + 1 < n && j + 1 < n)
Matrix[i + 1, j + 1] += exceededwater / 2;
}
}
return ans;
}
public static void Main()
{
int N = 3;
int T = 4;
Console.WriteLine(FindNoOfFullVessels(N, T));
}
}
|
Javascript
<script>
var n, t;
function FindNoOfFullVessels(n, t)
{
var Matrix = Array.from(Array(n), ()=> Array(n).fill(0));
Matrix[0][0] = t * 1.0;
var ans = 0;
for ( var i = 0; i < n; i++)
{
for ( var j = 0; j <= i; j++)
{
var exceededwater = Matrix[i][j] - 1;
if (exceededwater < 0)
continue ;
ans++;
if (i + 1 < n)
Matrix[i + 1][j] += (exceededwater / 2);
if (i + 1 < n && j + 1 < n)
Matrix[i + 1][j + 1] += (exceededwater / 2);
}
}
return ans;
}
var N = 3;
var T = 4;
document.write( FindNoOfFullVessels(N, T));
</script>
|
Output:
3
Time complexity: O(N2)
Space Complexity: O(N2)
Share your thoughts in the comments
Please Login to comment...