Find if the glass will be empty or not when the rate of drinking is given
Last Updated :
03 Mar, 2022
Given a cylinder glass with diameter equals D centimeters. The initial level of water in the glass equals H centimeters from the bottom. You drink the water with a speed of M milliliters per second. But if you do not drink the water from the glass, the level of water increases by N centimeters per second. The task is to find the time required to make the glass empty or find if it is possible to make the glass empty or not.
Examples:
Input: D = 1, H = 1, M = 1, N = 1
Output: 3.65979
Input: D = 1, H = 2, M = 3, N = 100
Output: -1
Approach: This is a Geometry question. It is known that the area of the glass is pie * r2 where r represents the radius i.e. (D / 2). So to find the rate at which the water is being consumed per second, divide the volume given (it is known that 1 milliliter equals 1 cubic centimeter) with the area.
If the value is less than the rate at which the water is being poured in the glass, if it is not being drunk then the answer will be No else the glass can be empty.
To find the time, divide h / (v / (pie * r2) – e) and that is the time when the glass will become empty.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
double pie = 3.1415926535897;
double findsolution( double d, double h,
double m, double n)
{
double k = (4 * m) / (pie * d * d);
if (n > k)
return -1;
double ans = (h / (k - n));
return ans;
}
int main()
{
double d = 1, h = 1, m = 1, n = 1;
cout << findsolution(d, h, m, n);
return 0;
}
|
Java
class GFG
{
static double pie = 3.1415926535897 ;
static double findsolution( double d, double h,
double m, double n)
{
double k = ( 4 * m) / (pie * d * d);
if (n > k)
return - 1 ;
double ans = (h / (k - n));
return ans;
}
public static void main(String[] args)
{
double d = 1 , h = 1 , m = 1 , n = 1 ;
System.out.printf( "%.5f" ,findsolution(d, h, m, n));
}
}
|
Python3
pie = 3.1415926535897
def findsolution(d, h, m, n):
k = ( 4 * m) / (pie * d * d)
if (n > k):
return - 1
ans = (h / (k - n))
return round (ans, 5 )
d = 1
h = 1
m = 1
n = 1
print (findsolution(d, h, m, n))
|
C#
using System;
class GFG
{
static double pie = 3.1415926535897;
static double findsolution( double d, double h,
double m, double n)
{
double k = (4 * m) / (pie * d * d);
if (n > k)
return -1;
double ans = (h / (k - n));
return ans;
}
public static void Main(String[] args)
{
double d = 1, h = 1, m = 1, n = 1;
Console.Write( "{0:F5}" ,
findsolution(d, h, m, n));
}
}
|
Javascript
<script>
const pie = 3.1415926535897;
function findsolution(d , h , m , n) {
var k = (4 * m) / (pie * d * d);
if (n > k)
return -1;
var ans = (h / (k - n));
return ans;
}
var d = 1, h = 1, m = 1, n = 1;
document.write(findsolution(d, h, m, n).toFixed(5));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...