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 * r ^{2}** 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 * r**and that is the time when the glass will become empty.

^{2}) – e)Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `double` `pie = 3.1415926535897; ` ` ` `// Function to return the time when ` `// the glass will be empty ` `double` `findsolution(` `double` `d, ` `double` `h, ` ` ` `double` `m, ` `double` `n) ` `{ ` ` ` `double` `k = (4 * m) / (pie * d * d); ` ` ` ` ` `// Check the condition when the ` ` ` `// glass will never be empty ` ` ` `if` `(n > k) ` ` ` `return` `-1; ` ` ` ` ` `// Find the time ` ` ` `double` `ans = (h / (k - n)); ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `double` `d = 1, h = 1, m = 1, n = 1; ` ` ` ` ` `cout << findsolution(d, h, m, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` ` ` `class` `GFG ` `{ ` ` ` `static` `double` `pie = ` `3.1415926535897` `; ` ` ` `// Function to return the time when ` `// the glass will be empty ` `static` `double` `findsolution(` `double` `d, ` `double` `h, ` ` ` `double` `m, ` `double` `n) ` `{ ` ` ` `double` `k = (` `4` `* m) / (pie * d * d); ` ` ` ` ` `// Check the condition when the ` ` ` `// glass will never be empty ` ` ` `if` `(n > k) ` ` ` `return` `-` `1` `; ` ` ` ` ` `// Find the time ` ` ` `double` `ans = (h / (k - n)); ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `double` `d = ` `1` `, h = ` `1` `, m = ` `1` `, n = ` `1` `; ` ` ` ` ` `System.out.printf(` `"%.5f"` `,findsolution(d, h, m, n)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` `pie ` `=` `3.1415926535897` ` ` `# Function to return the time when ` `# the glass will be empty ` `def` `findsolution(d, h, m, n): ` ` ` ` ` `k ` `=` `(` `4` `*` `m) ` `/` `(pie ` `*` `d ` `*` `d) ` ` ` ` ` `# Check the condition when the ` ` ` `# glass will never be empty ` ` ` `if` `(n > k): ` ` ` `return` `-` `1` ` ` ` ` `# Find the time ` ` ` `ans ` `=` `(h ` `/` `(k ` `-` `n)) ` ` ` `return` `round` `(ans, ` `5` `) ` ` ` `# Driver code ` `d ` `=` `1` `h ` `=` `1` `m ` `=` `1` `n ` `=` `1` ` ` `print` `(findsolution(d, h, m, n)) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` `static` `double` `pie = 3.1415926535897; ` ` ` `// Function to return the time when ` `// the glass will be empty ` `static` `double` `findsolution(` `double` `d, ` `double` `h, ` ` ` `double` `m, ` `double` `n) ` `{ ` ` ` `double` `k = (4 * m) / (pie * d * d); ` ` ` ` ` `// Check the condition when the ` ` ` `// glass will never be empty ` ` ` `if` `(n > k) ` ` ` `return` `-1; ` ` ` ` ` `// Find the time ` ` ` `double` `ans = (h / (k - n)); ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `double` `d = 1, h = 1, m = 1, n = 1; ` ` ` ` ` `Console.Write(` `"{0:F5}"` `, ` ` ` `findsolution(d, h, m, n)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

3.65979

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Program to find amount of water in a given glass
- Maximum sum of cocktail glass in a 2D matrix
- Program to find the rate percentage from compound interest of consecutive years
- Count of a, b & c after n seconds for given reproduction rate
- How to calculate strike rate of a batsman
- Cost required to empty a given array by repeated removal of maximum obtained by given operations
- Sum of values of all possible non-empty subsets of the given array
- Product of values of all possible non-empty subsets of given Array
- Sum of indices of Characters removed to obtain an Empty String based on given conditions
- Minimize cost to empty a given string by removing characters alphabetically
- Maximize count of empty water bottles from N filled bottles
- Number of ways of distributing N identical objects in R distinct groups with no groups empty
- Minimum palindromic subarray removals to make array Empty
- Size of all connected non-empty cells of a Matrix
- Partition a set into two non-empty subsets such that the difference of subset sums is maximum
- Find whether a given number is a power of 4 or not
- Find whether a given integer is a power of 3 or not
- Given a number x , find y such that x*y + 1 is not a prime
- Find if the given number is present in the infinite sequence or not
- Find the smallest positive number which can not be represented by given digits

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.