Given the **length**, **breadth**, **height** of a cuboid. The task is to divide the given cuboid in minimum number of cubes such that size of all cubes is same and sum of volumes of cubes is maximum.

Examples:

Input : l = 2, b = 4, h = 6 Output : 2 6 A cuboid of length 2, breadth 4 and height 6 can be divided into 6 cube of side equal to 2. Volume of cubes = 6*(2*2*2) = 6*8 = 48. Volume of cuboid = 2*4*6 = 48. Input : 1 2 3 Output : 1 6

First of all, we are not allowed to waste volume of cuboid as we meed maximum volume sum. So, each side should be completely divide among all cubes. And since each of three side of cubes are equal, so each side of the cuboid need to be divisible by same number, say x, which will going to be the side of the cube. So, we have to maximize this x, which will divide given length, breadth and height. This x will be maximum only if it is greatest common divisor of given length, breadth and height. So, the length of the cube will be GCD of length, breadth and height.

Now, to compute number of cubes, we know total volume of cuboid and can find volume of one cube (since side is already calculated). So, total number of cubes is equal to (volume of cuboid)/(volume of cube) i.e (l * b * h)/(x * x * x).

Below is implementation of this approach:

## C++

`// CPP program to find optimal way to break ` `// cuboid into cubes. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Print the maximum side and no of cube. ` `void` `maximizecube(` `int` `l, ` `int` `b, ` `int` `h) ` `{ ` ` ` `// GCD to find side. ` ` ` `int` `side = __gcd(l, __gcd(b, h)); ` ` ` ` ` `// dividing to find number of cubes. ` ` ` `int` `num = l / side; ` ` ` `num = (num * b / side); ` ` ` `num = (num * h / side); ` ` ` ` ` `cout << side << ` `" "` `<< num << endl; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `l = 2, b = 4, h = 6; ` ` ` ` ` `maximizecube(l, b, h); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// JAVA Code for Divide cuboid into cubes ` `// such that sum of volumes is maximum ` `import` `java.util.*; ` ` ` `class` `GFG { ` ` ` ` ` `static` `int` `gcd(` `int` `m, ` `int` `n) ` ` ` `{ ` ` ` `if` `(n == ` `0` `) ` ` ` `return` `m; ` ` ` `else` `if` `(n > m) ` ` ` `return` `gcd(n,m); ` ` ` `else` ` ` `return` `gcd(n, m % n); ` ` ` `} ` ` ` ` ` `// Print the maximum side and no ` ` ` `// of cube. ` ` ` `static` `void` `maximizecube(` `int` `l, ` `int` `b, ` ` ` `int` `h) ` ` ` `{ ` ` ` `// GCD to find side. ` ` ` `int` `side = gcd(l, gcd(b, h)); ` ` ` ` ` `// dividing to find number of cubes. ` ` ` `int` `num = l / side; ` ` ` `num = (num * b / side); ` ` ` `num = (num * h / side); ` ` ` ` ` `System.out.println( side + ` `" "` `+ num); ` ` ` `} ` ` ` ` ` `/* Driver program */` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `l = ` `2` `, b = ` `4` `, h = ` `6` `; ` ` ` `maximizecube(l, b, h); ` ` ` `} ` `} ` ` ` `// This code is contributed by Arnav Kr. Mandal. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 code to find optimal way to break ` `# cuboid into cubes. ` `from` `fractions ` `import` `gcd ` ` ` `# Print the maximum side and no of cube. ` `def` `maximizecube( l , b , h ): ` ` ` ` ` `# GCD to find side. ` ` ` `side ` `=` `gcd(l, gcd(b, h)) ` ` ` ` ` `# dividing to find number of cubes. ` ` ` `num ` `=` `int` `(l ` `/` `side) ` ` ` `num ` `=` `int` `(num ` `*` `b ` `/` `side) ` ` ` `num ` `=` `int` `(num ` `*` `h ` `/` `side) ` ` ` ` ` `print` `(side, num) ` ` ` `# Driver code ` `l ` `=` `2` `b ` `=` `4` `h ` `=` `6` ` ` `maximizecube(l, b, h) ` ` ` `# This code is contributed by "Sharad_Bhardwaj". ` |

*chevron_right*

*filter_none*

## C#

`// C# Code for Divide cuboid into cubes ` `// such that sum of volumes is maximum ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `static` `int` `gcd(` `int` `m, ` `int` `n) ` ` ` `{ ` ` ` `if` `(n == 0) ` ` ` `return` `m; ` ` ` `else` `if` `(n > m) ` ` ` `return` `gcd(n,m); ` ` ` `else` ` ` `return` `gcd(n, m % n); ` ` ` `} ` ` ` ` ` `// Print the maximum side and no ` ` ` `// of cube. ` ` ` `static` `void` `maximizecube(` `int` `l, ` `int` `b, ` ` ` `int` `h) ` ` ` `{ ` ` ` `// GCD to find side. ` ` ` `int` `side = gcd(l, gcd(b, h)); ` ` ` ` ` `// dividing to find number of cubes. ` ` ` `int` `num = l / side; ` ` ` `num = (num * b / side); ` ` ` `num = (num * h / side); ` ` ` ` ` `Console.WriteLine( side + ` `" "` `+ num); ` ` ` `} ` ` ` ` ` `/* Driver program */` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `l = 2, b = 4, h = 6; ` ` ` `maximizecube(l, b, h); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find optimal way ` `// to break cuboid into cubes. ` ` ` `// Recursive function to ` `// return gcd of a and b ` `function` `__gcd(` `$a` `, ` `$b` `) ` `{ ` ` ` ` ` `// Everything divides 0 ` ` ` `if` `(` `$a` `== 0 ` `or` `$b` `== 0) ` ` ` `return` `0 ; ` ` ` ` ` `// base case ` ` ` `if` `(` `$a` `== ` `$b` `) ` ` ` `return` `$a` `; ` ` ` ` ` `// a is greater ` ` ` `if` `(` `$a` `> ` `$b` `) ` ` ` `return` `__gcd(` `$a` `- ` `$b` `, ` `$b` `) ; ` ` ` ` ` `return` `__gcd(` `$a` `, ` `$b` `- ` `$a` `) ; ` `} ` ` ` ` ` `// Print the maximum side and no of cube. ` `function` `maximizecube(` `$l` `, ` `$b` `, ` `$h` `) ` `{ ` ` ` ` ` `// GCD to find side. ` ` ` `$side` `= __gcd(` `$l` `, __gcd(` `$b` `, ` `$h` `)); ` ` ` ` ` `// dividing to find number of cubes. ` ` ` `$num` `= ` `$l` `/ ` `$side` `; ` ` ` `$num` `= (` `$num` `* ` `$b` `/ ` `$side` `); ` ` ` `$num` `= (` `$num` `* ` `$h` `/ ` `$side` `); ` ` ` ` ` `echo` `$side` `, ` `" "` `, ` `$num` `; ` `} ` ` ` ` ` `// Driver code ` ` ` `$l` `= 2; ` ` ` `$b` `= 4; ` ` ` `$h` `= 6; ` ` ` `maximizecube(` `$l` `, ` `$b` `, ` `$h` `); ` ` ` `// This code is contributed by anuj_67. ` `?> ` |

*chevron_right*

*filter_none*

Output:

2 6

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Length of longest rod that can fit into a cuboid
- Find maximum volume of a cuboid from the given perimeter and area
- Maximize volume of cuboid with given sum of sides
- Program for Volume and Surface Area of Cuboid
- Divide 1 to n into two groups with minimum sum difference
- Check if a number can be represented as sum of two consecutive perfect cubes
- Check if a line at 45 degree can divide the plane into two equal weight parts
- Minimum cuts required to divide the Circle into equal parts
- Divide a number into two parts
- Divide array into increasing and decreasing subsequence without changing the order
- Split the given array into K sub-arrays such that maximum sum of all sub arrays is minimum
- Count cubes of size K inscribed in a cube of size N
- Partition into two subarrays of lengths k and (N - k) such that the difference of sums is maximum
- Partition an array such into maximum increasing segments
- Partition a set into two non-empty subsets such that the difference of subset sums is maximum
- Maximum number of region in which N non-parallel lines can divide a plane
- Partition first N natural number into two sets such that their sum is not coprime
- Split N powers of 2 into two subsets such that their difference of sum is minimum
- Maximum sub-array sum after dividing array into sub-arrays based on the given queries
- Split array into minimum number of subsets having maximum pair sum at most K

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.