Minimum number of Cuboids required to form a Cube
Given L, B, and H which denotes the length, breadth, and height of a cuboid, the task is to find the minimum number of cuboids of specified dimensions that can be placed together to form a cube.
Examples:
Input: L = 1, B = 1, H = 2
Output: 4
Explanation:
Volume of a cuboid of given dimensions = 1 * 1 * 2 = 2.
Volume of the cube that can be formed by combining these cuboids = 2 * 2 * 2 = 8.
Therefore, the number of cuboids required = 8 / 2 = 4.
Input: L = 2, B = 5, H = 10
Output: 10
Naive Approach: Find the maximum of the given dimensions and start iterating over integer values starting from the obtained maximum. For every integer, check if it can be a possible dimension of a cube that can be formed by the given cuboids or not. In order to do so, calculate the volume of the cube and the volume of the cuboid formed by given dimensions. Check if former is divisible by the latter or not. If found to be true, then print the quotient as the required answer.
Time Complexity: O(L * B * H)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is based on the following observation:
- The minimum length of a cube obtained by combining cuboids of given dimensions is equal to LCM of L, B, and H. This is because the dimension of the cube must be divisible by L, B, and H.
- In order to find the number of cuboids required, calculate the volume of the cube ( = LCM(L, B, H)3) and the cuboid ( = L * B * H) and print ( Volume of cube ) / ( Volume of cuboid ) a the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int find_lcm( int a, int b, int c)
{
int g = __gcd(a, b);
int LCM1 = (a * b) / g;
g = __gcd(LCM1, c);
int LCM = (LCM1 * c) / g;
return LCM;
}
void minimumCuboids( int L, int B, int H)
{
int lcm = find_lcm(L, B, H);
int volume_cube = lcm * lcm * lcm;
int volume_cuboid = L * B * H;
cout << (volume_cube / volume_cuboid);
}
int main()
{
int L = 1, B = 1, H = 2;
minimumCuboids(L, B, H);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int find_lcm( int a, int b, int c)
{
int g = __gcd(a, b);
int LCM1 = (a * b) / g;
g = __gcd(LCM1, c);
int LCM = (LCM1 * c) / g;
return LCM;
}
static void minimumCuboids( int L, int B, int H)
{
int lcm = find_lcm(L, B, H);
int volume_cube = lcm * lcm * lcm;
int volume_cuboid = L * B * H;
System.out.print((volume_cube / volume_cuboid));
}
static int __gcd( int a, int b)
{
return b == 0 ? a:__gcd(b, a % b);
}
public static void main(String[] args)
{
int L = 1 , B = 1 , H = 2 ;
minimumCuboids(L, B, H);
}
}
|
Python3
def find_lcm(a, b, c):
g = __gcd(a, b);
LCM1 = (a * b) / / g;
g = __gcd(LCM1, c);
LCM = (LCM1 * c) / / g;
return LCM;
def minimumCuboids(L, B, H):
lcm = find_lcm(L, B, H);
volume_cube = lcm * lcm * lcm;
volume_cuboid = L * B * H;
print ((volume_cube / / volume_cuboid));
def __gcd(a, b):
if (b = = 0 ):
return a;
else :
return __gcd(b, a % b);
if __name__ = = '__main__' :
L = 1 ; B = 1 ; H = 2 ;
minimumCuboids(L, B, H);
|
C#
using System;
class GFG
{
static int find_lcm( int a, int b, int c)
{
int g = __gcd(a, b);
int LCM1 = (a * b) / g;
g = __gcd(LCM1, c);
int LCM = (LCM1 * c) / g;
return LCM;
}
static void minimumCuboids( int L, int B, int H)
{
int lcm = find_lcm(L, B, H);
int volume_cube = lcm * lcm * lcm;
int volume_cuboid = L * B * H;
Console.Write((volume_cube / volume_cuboid));
}
static int __gcd( int a, int b)
{
return b == 0 ? a:__gcd(b, a % b);
}
public static void Main(String[] args)
{
int L = 1, B = 1, H = 2;
minimumCuboids(L, B, H);
}
}
|
Javascript
<script>
function find_lcm(a, b, c)
{
let g = __gcd(a, b);
let LCM1 = (a * b) / g;
g = __gcd(LCM1, c);
let LCM = (LCM1 * c) / g;
return LCM;
}
function minimumCuboids(L, B, H)
{
let lcm = find_lcm(L, B, H);
let volume_cube = lcm * lcm * lcm;
let volume_cuboid = L * B * H;
document.write((volume_cube /
volume_cuboid));
}
function __gcd(a, b)
{
return b == 0 ? a : __gcd(b, a % b);
}
let L = 1, B = 1, H = 2;
minimumCuboids(L, B, H);
</script>
|
Time Complexity: O(log(min(L, B, H)))
Auxiliary Space: O(1)
Last Updated :
07 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...