Find the maximum angle at which we can tilt the bottle without spilling any water
Given a water bottle with the shape of a rectangular prism whose base is a square of side x cm and height y cm, the task is to find the maximum angle at which we can tilt the bottle without spilling any water, when z cubic cm of water is poured into the bottle, and gradually tilt the bottle around one of the sides of the base.
Examples:
Input: x = 2, y = 2, z = 4
Output: 45.00000
Explanation:
This bottle has a cubic shape,
and it is half-full.
The water gets split when we tilt
the bottle more than 45 degrees.
Input: x = 12, y = 21, z = 10
Output: 89.78346
Approach:
First of all, we need to know that the surface of the water is always parallel to the ground. So if the bottle tilts the water remain parallel to the ground.
Here we have 2 cases:-
- When the water is less than half of the total volume of the bottle, Then we consider the lower right triangle formed by water inside the bottle and with the help of tangent inverse calculate the required angle.
- When the water is greater than or equal to the total volume of the bottle, then we consider the upper right triangle formed by empty space inside the bottle. Now we can calculate the angle easily.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
float find_angle( int x, int y, int z)
{
int volume = x * x * y;
float ans = 0;
if (z < volume / 2) {
float d = (x * y * y) / (2.0 * z);
ans = atan (d);
}
else {
z = volume - z;
float d = (2 * z) / ( float )(x * x * x);
ans = atan (d);
}
ans = (ans * 180) / 3.14159265;
return ans;
}
int main()
{
int x = 12;
int y = 21;
int z = 10;
cout << find_angle(x, y, z) << endl;
}
|
Java
class GFG
{
static float find_angle( int x,
int y, int z)
{
int volume = x * x * y;
float ans = 0 ;
if (z < volume / 2 )
{
float d = ( float ) ((x * y * y) / ( 2.0 * z));
ans = ( float ) Math.atan(d);
}
else
{
z = volume - z;
float d = ( 2 * z) / ( float ) (x * x * x);
ans = ( float ) Math.atan(d);
}
ans = ( float ) ((ans * 180 ) / 3.14159265 );
return ans;
}
public static void main(String[] args)
{
int x = 12 ;
int y = 21 ;
int z = 10 ;
System.out.print(find_angle(x, y, z) + "\n" );
}
}
|
Python3
from math import *
def find_angle(x, y, z) :
volume = x * x * y;
ans = 0 ;
if (z < volume / / 2 ) :
d = (x * y * y) / ( 2.0 * z);
ans = atan(d);
else :
z = volume - z;
d = ( 2 * z) / ( float )(x * x * x);
ans = atan(d);
ans = (ans * 180 ) / 3.14159265 ;
return round (ans, 4 );
if __name__ = = "__main__" :
x = 12 ;
y = 21 ;
z = 10 ;
print (find_angle(x, y, z));
|
C#
using System;
class GFG
{
static float find_angle( int x,
int y, int z)
{
int volume = x * x * y;
float ans = 0;
if (z < volume / 2)
{
float d = ( float ) ((x * y * y) / (2.0 * z));
ans = ( float ) Math.Atan(d);
}
else
{
z = volume - z;
float d = (2 * z) / ( float ) (x * x * x);
ans = ( float ) Math.Atan(d);
}
ans = ( float ) ((ans * 180) / 3.14159265);
return ans;
}
public static void Main(String[] args)
{
int x = 12;
int y = 21;
int z = 10;
Console.Write(find_angle(x, y, z) + "\n" );
}
}
|
Javascript
<script>
function find_angle(x , y , z) {
var volume = x * x * y;
var ans = 0;
if (z < volume / 2) {
var d = ((x * y * y) / (2.0 * z));
ans = Math.atan(d);
} else {
z = volume - z;
var d = (2 * z) / (x * x * x);
ans = Math.atan(d);
}
ans = ((ans * 180) / 3.14159265);
return ans;
}
var x = 12;
var y = 21;
var z = 10;
document.write(find_angle(x, y, z).toFixed(4) + "\n" );
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
26 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...