Maximize volume of cuboid with given sum of sides
We are given the sum of length, breadth and height, say S, of a cuboid. The task is to find the maximum volume that can be achieved so that sum of side is S.
Volume of a cuboid = length * breadth * height
Examples :
Input : s = 4 Output : 2 Only possible dimensions are some combination of 1, 1, 2. Input : s = 8 Output : 18 All possible edge dimensions: [1, 1, 6], volume = 6 [1, 2, 5], volume = 10 [1, 3, 4], volume = 12 [2, 2, 4], volume = 16 [2, 3, 3], volume = 18
Method 1: (Brute Force)
The idea to run three nested, one for length, one for breadth and one for height. For each iteration, calculate the volume and compare with maximum volume.
Below is the implementation of this approach:
C++
#include <bits/stdc++.h> using namespace std; // Return the maximum volume. int maxvolume(int s) { int maxvalue = 0; // for length for (int i = 1; i <= s - 2; i++) { // for breadth for (int j = 1; j <= s - 1; j++) { // for height int k = s - i - j; // calculating maximum volume. maxvalue = max(maxvalue, i * j * k); } } return maxvalue; } // Driven Program int main() { int s = 8; cout << maxvolume(s) << endl; return 0; } |
Java
// Java code to Maximize volume of // cuboid with given sum of sides class GFG { // Return the maximum volume. static int maxvolume(int s) { int maxvalue = 0; // for length for (int i = 1; i <= s - 2; i++) { // for breadth for (int j = 1; j <= s - 1; j++) { // for height int k = s - i - j; // calculating maximum volume. maxvalue = Math.max(maxvalue, i * j * k); } } return maxvalue; } // Driver function public static void main (String[] args) { int s = 8; System.out.println(maxvolume(s)); } } // This code is contributed by Anant Agarwal. |
Python3
# Python3 code to Maximize volume of # cuboid with given sum of sides # Return the maximum volume. def maxvolume (s): maxvalue = 0 # for length i = 1 for i in range(s - 1): j = 1 # for breadth for j in range(s): # for height k = s - i - j # calculating maximum volume. maxvalue = max(maxvalue, i * j * k) return maxvalue # Driven Program s = 8print(maxvolume(s)) # This code is contributed by "Sharad_Bhardwaj". |
C#
// C# code to Maximize volume of // cuboid with given sum of sides using System; class GFG { // Return the maximum volume. static int maxvolume(int s) { int maxvalue = 0; // for length for (int i = 1; i <= s - 2; i++) { // for breadth for (int j = 1; j <= s - 1; j++) { // for height int k = s - i - j; // calculating maximum volume. maxvalue = Math.Max(maxvalue, i * j * k); } } return maxvalue; } // Driver function public static void Main () { int s = 8; Console.WriteLine(maxvolume(s)); } } // This code is contributed by vt_m. |
PHP
<?php // PHP code to Maximize volume of // cuboid with given sum of sides // Return the maximum volume. function maxvolume( $s) { $maxvalue = 0; // for length for ( $i = 1; $i <= $s - 2; $i++) { // for breadth for ( $j = 1; $j <= $s - 1; $j++) { // for height $k = $s - $i - $j; // calculating maximum volume. $maxvalue = max($maxvalue, $i * $j * $k); } } return $maxvalue; } // Driver Code $s = 8; echo(maxvolume($s)); // This code is contributed by vt_m. ?> |
Output :
18
Time Complexity: O(n2)
Method 2: (Efficient approach)
The idea is to divide edges as equally as possible.
So,
length = floor(s/3)
width = floor((s – length)/2) = floor((s – floor(s/3)/2)
height = s – length – width = s – floor(s/3) – floor((s – floor(s/3))/2)
Below is the implementation of this approach:
C++
#include <bits/stdc++.h> using namespace std; // Return the maximum volume. int maxvolume(int s) { // finding length int length = s / 3; s -= length; // finding breadth int breadth = s / 2; // finding height int height = s - breadth; return length * breadth * height; } // Driven Program int main() { int s = 8; cout << maxvolume(s) << endl; return 0; } |
Java
// Java code to Maximize volume of // cuboid with given sum of sides import java.io.*; class GFG { // Return the maximum volume. static int maxvolume(int s) { // finding length int length = s / 3; s -= length; // finding breadth int breadth = s / 2; // finding height int height = s - breadth; return length * breadth * height; } // Driven Program public static void main (String[] args) { int s = 8; System.out.println ( maxvolume(s)); } } // This code is contributed by vt_m. |
Python3
# Python3 code to Maximize volume of # cuboid with given sum of sides # Return the maximum volume. def maxvolume( s ): # finding length length = int(s / 3) s -= length # finding breadth breadth = s / 2 # finding height height = s - breadth return int(length * breadth * height) # Driven Program s = 8print( maxvolume(s) ) # This code is contributed by "Sharad_Bhardwaj". |
C#
// C# code to Maximize volume of // cuboid with given sum of sides using System; class GFG { // Return the maximum volume. static int maxvolume(int s) { // finding length int length = s / 3; s -= length; // finding breadth int breadth = s / 2; // finding height int height = s - breadth; return length * breadth * height; } // Driven Program public static void Main () { int s = 8; Console.WriteLine( maxvolume(s)); } } // This code is contributed by vt_m. |
PHP
<?php // Return the maximum volume. function maxvolume($s) { // finding length $length = (int)($s / 3); $s -= $length; // finding breadth $breadth = (int)($s / 2); // finding height $height = $s - $breadth; return $length * $breadth * $height; } // Driven Code $s = 8; echo(maxvolume($s)); // This code is contributed by Ajit. ?> |
Output :
18
Time Complexity: O(1)
How does this work?
We basically need to maximize product of
three numbers, x, y and z whose sum is given.Given s = x + y + z
Maximize P = x * y * z
= x * y * (s – x – y)
= x*y*s – x*x*s – x*y*yWe get dp/dx = sy – 2xy – y*y
and dp/dy = sx – 2xy – x*xWe get dp/dx = 0 and dp/dy = 0 when
x = s/3, y = s/3So z = s – x – y = s/3
Recommended Posts:
- Program for Volume and Surface Area of Cuboid
- Find maximum volume of a cuboid from the given perimeter and area
- Percentage increase in the volume of cuboid if length, breadth and height are increased by fixed percentages
- Number of triangles formed by joining vertices of n-sided polygon with two common sides and no common sides
- Number of ways to arrange 2*N persons on the two sides of a table with X and Y persons on opposite sides
- Length of longest rod that can fit into a cuboid
- Divide cuboid into cubes such that sum of volumes is maximum
- Python | Percentage increase in the total surface area of the cuboid
- Check if it is possible to create a polygon with given n sides
- Find other two sides of a right angle triangle
- Check whether triangle is valid or not if sides are given
- Area of the circumcircle of any triangles with sides given
- Check if given four integers (or sides) make rectangle
- Count of acute, obtuse and right triangles with given sides
- Find other two sides and angles of a right angle triangle
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



