Find maximum volume of a cuboid from the given perimeter and area

07 Sep, 2021
Given a perimeter P and area A, the task is to calculate the maximum volume that can be made in form of cuboid from the given perimeter and surface area.
Examples : 
Input: P = 24, A = 24
Output: 8

Input: P = 20, A = 14
Output: 3
Approach: For a given perimeter of cuboid we have P = 4(l+b+h) —(i), for given area of cuboid we have A = 2 (lb+bh+lh) —(ii). Volume of cuboid is V = lbhVolume is dependent on 3 variables l, b, h. Lets make it dependent on only length.
as V = lbh, => V = l (A/2-(lb+lh)) {from equation (ii)} => V = lA/2 – l2(b+h) => V = lA/2 – l2(P/4-l) {from equation (i)} => V = lA/2 – l2P/4 + l3 —-(iii)Now differentiate V w.r.t l for finding maximum of volume. dV/dl = A/2 – lP/2 + 3l2 After solving the quadratic in l we have l = (P – (P2-24A)1/2) / 12 Substituting value of l in (iii), we can easily find the maximum volume.
Below is the implementation of the above approach:
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation of the above approach#include <bits/stdc++.h>using namespace std; // function to return maximum volumefloat maxVol(float P, float A){    // calculate length    float l = (P - sqrt(P * P - 24 * A)) / 12;     // calculate volume    float V = l * (A / 2.0 - l * (P / 4.0 - l));     // return result    return V;} // Driver codeint main(){    float P = 20, A = 16;       // Function call    cout << maxVol(P, A);     return 0;}
// Java implementation of the above approachimport java.util.*; class Geeks {     // function to return maximum volume    static float maxVol(float P, float A)    {        // calculate length        float l            = (float)(P - Math.sqrt(P * P - 24 * A)) / 12;         // calculate volume        float V            = (float)(l * (A / 2.0 - l * (P / 4.0 - l)));         // return result        return V;    }     // Driver code    public static void main(String args[])    {        float P = 20, A = 16;               // Function call        System.out.println(maxVol(P, A));    }} // This code is contributed by Kirti_Mangal
# Python3 implementation of the# above approachfrom math import sqrt # function to return maximum volume  def maxVol(P, A):     # calculate length    l = (P - sqrt(P * P - 24 * A)) / 12     # calculate volume    V = l * (A / 2.0 - l * (P / 4.0 - l))     # return result    return V  # Driver codeif __name__ == '__main__':    P = 20    A = 16         # Function call    print(maxVol(P, A)) # This code is contributed# by Surendra_Gangwar
// C# implementation of the above approachusing System; class GFG {     // function to return maximum volume    static float maxVol(float P, float A)    {        // calculate length        float l            = (float)(P - Math.Sqrt(P * P - 24 * A)) / 12;         // calculate volume        float V            = (float)(l * (A / 2.0 - l * (P / 4.0 - l)));         // return result        return V;    }     // Driver code    public static void Main()    {        float P = 20, A = 16;                // Function call        Console.WriteLine(maxVol(P, A));    }} // This code is contributed// by Akanksha Rai
<?php// PHP implementation of the above approach // function to return maximum volumefunction maxVol($P, $A){    // calculate length    $l = ($P - sqrt($P * $P - 24 * $A)) / 12;     // calculate volume    $V = $l * ($A / 2.0 - $l *              ($P / 4.0 - $l));     // return result    return $V;} // Driver code$P = 20;$A = 16; // Function callecho maxVol($P, $A); // This code is contributed by mits?>
<script>// javascript implementation of the above approach  // function to return maximum volumefunction maxVol( P,  A){     // calculate length    let l = (P - Math.sqrt(P * P - 24 * A)) / 12;     // calculate volume    let V = l * (A / 2.0 - l * (P / 4.0 - l));     // return result    return V;} // Driver code    let P = 20, A = 16;       // Function call    document.write(maxVol(P, A).toFixed(5));        // This code is contributed by aashish1995 </script>
4.14815
SURENDRA_GANGWAR
Kirti_Mangal
Akanksha_Rai
Mithun Kumar
abh1shekk
aashish1995
sweetyty
area-volume-programs
Geometric
Mathematical
Mathematical
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program for distance between two points on earth
Optimum location of point to minimize total distance
Check if two given circles touch or intersect each other
Convex Hull | Set 1 (Jarvis's Algorithm or Wrapping)
Line Clipping | Set 1 (Cohen–Sutherland Algorithm)
Program for Fibonacci numbers
Set in C++ Standard Template Library (STL)
Write a program to print all permutations of a given string
C++ Data Types
Merge two sorted arrays