Find amount of water wasted after filling the tank
Last Updated :
09 Jun, 2022
Given the volume V of a tank in liter. There is a pump which is filling the tank at speed of M liter per minute. There is a leakage at the bottom of the tank which wasting water at speed N liter per minute. Given N is less than M. The task is to calculate how much amount of water will be wasted if leakage is seen after filling the full tank.
Examples:
Input : V = 700, M = 10, N = 3
Output : 300
Input : V = 1000, M = 100, N = 50
Output : 1000
Approach : Given the speed of filling pump is M liter per minute. So, the amount of water filled in one minute is M Liter. Also, N litres of water is wasted in a minute. Therefore after one minute the amount of water in the tank will be (M – N). Hence total time taken to fill the tank with leakage will be V / (M-N).
Therefore the amount of wasted water will be (V / (M-N)) * N.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
double wastedWater( double V, double M, double N)
{
double wasted_amt, amt_per_min, time_to_fill;
amt_per_min = M - N;
time_to_fill = V / amt_per_min;
wasted_amt = N * time_to_fill;
return wasted_amt;
}
int main()
{
double V, M, N;
V = 700;
M = 10;
N = 3;
cout << wastedWater(V, M, N) << endl;
V = 1000;
M = 100;
N = 50;
cout << wastedWater(V, M, N) << endl;
return 0;
}
|
Java
class GFG
{
static double wastedWater( double V, double M, double N)
{
double wasted_amt, amt_per_min, time_to_fill;
amt_per_min = M - N;
time_to_fill = V / amt_per_min;
wasted_amt = N * time_to_fill;
return wasted_amt;
}
public static void main(String[] args)
{
double V, M, N;
V = 700 ;
M = 10 ;
N = 3 ;
System.out.println(wastedWater(V, M, N)) ;
V = 1000 ;
M = 100 ;
N = 50 ;
System.out.println(wastedWater(V, M, N));
}
}
|
Python3
def wastedWater(V, M, N):
amt_per_min = M - N
time_to_fill = V / amt_per_min
wasted_amt = N * time_to_fill
return wasted_amt
V = 700
M = 10
N = 3
print (wastedWater(V, M, N))
V = 1000
M = 100
N = 50
print (wastedWater(V, M, N))
|
C#
using System;
class GFG
{
static double wastedWater( double V, double M, double N)
{
double wasted_amt, amt_per_min, time_to_fill;
amt_per_min = M - N;
time_to_fill = V / amt_per_min;
wasted_amt = N * time_to_fill;
return wasted_amt;
}
public static void Main()
{
double V, M, N;
V = 700;
M = 10;
N = 3;
Console.WriteLine(wastedWater(V, M, N)) ;
V = 1000;
M = 100;
N = 50;
Console.WriteLine(wastedWater(V, M, N));
}
}
|
PHP
<?php
function wastedWater( $V , $M , $N )
{
$amt_per_min = $M - $N ;
$time_to_fill = $V / $amt_per_min ;
$wasted_amt = $N * $time_to_fill ;
return $wasted_amt ;
}
$V = 700;
$M = 10;
$N = 3;
echo wastedWater( $V , $M , $N ), "\n" ;
$V = 1000;
$M = 100;
$N = 50;
echo wastedWater( $V , $M , $N );
?>
|
Javascript
<script>
function wastedWater(V, M, N)
{
let wasted_amt, amt_per_min, time_to_fill;
amt_per_min = M - N;
time_to_fill = V / amt_per_min;
wasted_amt = N * time_to_fill;
return wasted_amt;
}
let V, M, N;
V = 700;
M = 10;
N = 3;
document.write(wastedWater(V, M, N), "<br>" ) ;
V = 1000;
M = 100;
N = 50;
document.write(wastedWater(V, M, N));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...