Given a right circular cylinder which is inscribed in a cone of height h and base radius r. The task is to find the largest possible volume of the cylinder.
Input: r = 4, h = 8 Output: 119.087 Input: r = 5, h = 9 Output: 209.333
Approach: The volume of a cylinder is V = πr^2h
In this problem, first derive an equation for volume using similar triangles in terms of the height and radius of the cone. Once we have the modified the volume equation, we’ll take the derivative of the volume and solve for the largest value.
Let x be the radius of the cylinder and y be the distance from the top of the cone to the top of the inscribed cylinder. Therefore, the height of the cylinder is h – y
The volume of the inscribed cylinder is V = πx^2(h-y).
We use the method of similar ratios to find a relationship between the height and radius, h-y and x.
y/x = h/r
y = hx/r
Substitute the equation for y into the equation for volume, V.
V = πx^2(h-y)
V = πx^2(h-hx/r)
V = πx^2h – πx^3h/r
now, dV/dx = d(πx^2h – πx^3h/r)/dx
and setting dV/dx = 0
we get, x = 0, 2r/3
So, x = 2r/3
and, y = 2h/3
So, V = π8r^2h/27
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.