Given two integers p and a, where,
- p represents the perimeter
- a represents the surface area of a cuboid,Â
Find the maximum possible volume of the cuboid and return it rounded to 2 decimal places.
Note: It is guaranteed that a valid cuboid always exists for the given values.
Examples:
Input: p = 22, a = 15
Output: 3.02
Explanation: The maximum attainable volume of the cuboid is 3.02Input: p = 20, a = 5
Output: 0.33
Explanation: The maximum attainable volume of the cuboid is 0.33
[Expected Approach] Using Mathematical Formula - O(1) Time and O(1) Space
Let the dimensions of the cuboid be l, b, and h. Then:
2(lb + bh + lh) = a ...(i)
4(l + b + h) = p ...(ii)
Now Volume, V = lbh
From equation (i):
lb + bh + lh = a/2
l(b + h) + bh = a/2
bh = a/2 - l(b + h)
Therefore, V = l(a/2 - l(b + h))
From equation (ii):
l + b + h = p/4
b + h = p/4 - l
Hence,
V = la/2 - l²(b + h)
V = la/2 - l²(p/4 - l)
V = la/2 - l²p/4 + l³ ...(iii)
Now differentiate V with respect to l:
dV/dl = a/2 - lp/2 + 3l²
Setting it equal to 0:
a/2 - lp/2 + 3l² = 0
6l² - pl + a = 0
Thus,
l = (p ± √(p² - 24a)) / 12
Taking the valid value:
l = (p - √(p² - 24a)) / 12
Finally, substitute this value of l into equation (iii) to obtain the maximum volume.
#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
double maxVolume(int p, int a) {
// Calculate the two equal dimensions of the cuboid.
double x = (p - sqrt(1.0 * p * p - 24.0 * a)) / 12.0;
// Calculate the third dimension.
double y = p / 4.0 - 2.0 * x;
return x * x * y;
}
int main() {
int p1 = 22;
int a1 = 15;
cout << fixed << setprecision(2) << maxVolume(p1, a1) << "\n";
int p2 = 20;
int a2 = 5;
cout << fixed << setprecision(2) << maxVolume(p2, a2) << "\n";
return 0;
}
import java.lang.Math;
import java.text.DecimalFormat;
public class Main {
public static double maxVolume(int p, int a) {
// Calculate the two equal dimensions of the cuboid.
double x = (p - Math.sqrt(1.0 * p * p - 24.0 * a)) / 12.0;
// Calculate the third dimension.
double y = p / 4.0 - 2.0 * x;
return x * x * y;
}
public static void main(String[] args) {
int p1 = 22;
int a1 = 15;
DecimalFormat df = new DecimalFormat("#.##");
System.out.println(df.format(maxVolume(p1, a1)));
int p2 = 20;
int a2 = 5;
System.out.println(df.format(maxVolume(p2, a2)));
}
}
import math
def maxVolume(p, a):
# Calculate the two equal dimensions of the cuboid.
x = (p - math.sqrt(1.0 * p * p - 24.0 * a)) / 12.0
# Calculate the third dimension.
y = p / 4.0 - 2.0 * x
return x * x * y
p1 = 22
a1 = 15
print(f"{maxVolume(p1, a1):.2f}")
p2 = 20
a2 = 5
print(f"{maxVolume(p2, a2):.2f}")
using System;
class Program
{
static double maxVolume(int p, int a)
{
// Calculate the two equal dimensions of the cuboid.
double x = (p - Math.Sqrt(1.0 * p * p - 24.0 * a)) / 12.0;
// Calculate the third dimension.
double y = p / 4.0 - 2.0 * x;
return x * x * y;
}
static void Main()
{
int p1 = 22;
int a1 = 15;
Console.WriteLine(maxVolume(p1, a1).ToString("0.00"));
int p2 = 20;
int a2 = 5;
Console.WriteLine(maxVolume(p2, a2).ToString("0.00"));
}
}
function maxVolume(p, a) {
// Calculate the two equal dimensions of the cuboid.
let x = (p - Math.sqrt(1.0 * p * p - 24.0 * a)) / 12.0;
// Calculate the third dimension.
let y = p / 4.0 - 2.0 * x;
return x * x * y;
}
let p1 = 22;
let a1 = 15;
console.log(maxVolume(p1, a1).toFixed(2));
let p2 = 20;
let a2 = 5;
console.log(maxVolume(p2, a2).toFixed(2));
Output
3.02 0.33