Divide cuboid into cubes such that sum of volumes is maximum
Last Updated :
29 Feb, 2024
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 need 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++
#include <bits/stdc++.h>
using namespace std;
void maximizecube( int l, int b, int h)
{
int side = __gcd(l, __gcd(b, h));
int num = l / side;
num = (num * b / side);
num = (num * h / side);
cout << side << " " << num << endl;
}
int main()
{
int l = 2, b = 4, h = 6;
maximizecube(l, b, h);
return 0;
}
|
Java
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);
}
static void maximizecube( int l, int b,
int h)
{
int side = gcd(l, gcd(b, h));
int num = l / side;
num = (num * b / side);
num = (num * h / side);
System.out.println( side + " " + num);
}
public static void main(String[] args)
{
int l = 2 , b = 4 , h = 6 ;
maximizecube(l, b, h);
}
}
|
Python3
from fractions import gcd
def maximizecube( l , b , h ):
side = gcd(l, gcd(b, h))
num = int (l / side)
num = int (num * b / side)
num = int (num * h / side)
print (side, num)
l = 2
b = 4
h = 6
maximizecube(l, b, h)
|
C#
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);
}
static void maximizecube( int l, int b,
int h)
{
int side = gcd(l, gcd(b, h));
int num = l / side;
num = (num * b / side);
num = (num * h / side);
Console.WriteLine( side + " " + num);
}
public static void Main()
{
int l = 2, b = 4, h = 6;
maximizecube(l, b, h);
}
}
|
Javascript
<script>
function gcd(m, n)
{
if (n == 0)
return m;
else if (n > m)
return gcd(n,m);
else
return gcd(n, m % n);
}
function maximizecube(l, b, h)
{
let side = gcd(l, gcd(b, h));
let num = l / side;
num = (num * b / side);
num = (num * h / side);
document.write( side + " " + num);
}
let l = 2, b = 4, h = 6;
maximizecube(l, b, h);
</script>
|
PHP
<?php
function __gcd( $a , $b )
{
if ( $a == 0 or $b == 0)
return 0 ;
if ( $a == $b )
return $a ;
if ( $a > $b )
return __gcd( $a - $b , $b ) ;
return __gcd( $a , $b - $a ) ;
}
function maximizecube( $l , $b , $h )
{
$side = __gcd( $l , __gcd( $b , $h ));
$num = $l / $side ;
$num = ( $num * $b / $side );
$num = ( $num * $h / $side );
echo $side , " " , $num ;
}
$l = 2;
$b = 4;
$h = 6;
maximizecube( $l , $b , $h );
?>
|
Output:
2 6
Time Complexity: O(log2n), where n is the upper limit of b and h.
Auxiliary Space: O(1)
Please suggest if someone has a better solution which is more efficient in terms of space and time.
Share your thoughts in the comments
Please Login to comment...