You are given a perimeter & the area. Your task is to return the maximum volume that can be made in the form of a cuboid from the given perimeter and surface area.
Note: Round off to 2 decimal places and for the given parameters, it is guaranteed that an answer always exists.
Examples
Input: perimeter = 22, area = 15 Output: 3.02 Explanation: The maximum attainable volume of the cuboid is 3.02
Input: perimeter = 20, area = 5
Output: 0.33 Explanation: The maximum attainable volume of the cuboid is 0.33
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints :
1 ≤ perimeter ≤ 109
1 ≤ area ≤ 109
Approach 01:
-
C++
-
Python
#include <iostream> #include <cmath> #include <iomanip> class Solution { public: double maxVolume(double perimeter, double area) { // Calculate the adjusted perimeter double adjusted_perimeter = perimeter - std::sqrt(std::pow(perimeter, 2) - (24 * area)); // Calculate the squared adjusted perimeter double squared_adjusted_perimeter = std::pow((adjusted_perimeter / 12), 2); // Calculate the adjusted height double adjusted_height = (perimeter / 4) - (2 * (adjusted_perimeter / 12)); // Calculate the maximum volume double maximum_volume = squared_adjusted_perimeter * adjusted_height; // Return the maximum volume return maximum_volume; } };
import math class Solution: def maxVolume(self, perimeter, area): # Calculate the adjusted perimeter adjusted_perimeter = perimeter - math.sqrt(math.pow(perimeter, 2) - (24 * area)) # Calculate the squared adjusted perimeter squared_adjusted_perimeter = math.pow((adjusted_perimeter / 12), 2) # Calculate the adjusted height adjusted_height = (perimeter / 4) - (2 * (adjusted_perimeter / 12)) # Calculate the maximum volume maximum_volume = squared_adjusted_perimeter * adjusted_height # Return the maximum volume return maximum_volume