Minimum number of Cuboids required to form a Cube

07 Sep, 2021
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 = 2Output: 4Explanation: 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 = 10Output: 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++
Java
Python3
C#
Javascript
// C++ program for the above approach #include <bits/stdc++.h>using namespace std; // Function to calculate and// return LCM of a, b, and cint find_lcm(int a, int b, int c){    // Find GCD of a and b    int g = __gcd(a, b);     // Find LCM of a and b    int LCM1 = (a * b) / g;     // LCM(a, b, c) = LCM(LCM(a, b), c)    g = __gcd(LCM1, c);     // Finding LCM of a, b, c    int LCM = (LCM1 * c) / g;     // return LCM(a, b, c)    return LCM;} // Function to find the minimum// number of cuboids required to// make the volume of a valid cubevoid minimumCuboids(int L, int B, int H){    // Find the LCM of L, B, H    int lcm = find_lcm(L, B, H);     // Volume of the cube    int volume_cube = lcm * lcm * lcm;     // Volume of the cuboid    int volume_cuboid = L * B * H;     // Minimum number cuboids required    // to form a cube    cout << (volume_cube / volume_cuboid);} // Driver Codeint main(){    // Given dimensions of cuboid    int L = 1, B = 1, H = 2;     // Function Call    minimumCuboids(L, B, H);     return 0;}
// Java program for the above approachimport java.util.*;class GFG{ // Function to calculate and// return LCM of a, b, and cstatic int find_lcm(int a, int b, int c){       // Find GCD of a and b    int g = __gcd(a, b);     // Find LCM of a and b    int LCM1 = (a * b) / g;     // LCM(a, b, c) = LCM(LCM(a, b), c)    g = __gcd(LCM1, c);     // Finding LCM of a, b, c    int LCM = (LCM1 * c) / g;     // return LCM(a, b, c)    return LCM;} // Function to find the minimum// number of cuboids required to// make the volume of a valid cubestatic void minimumCuboids(int L, int B, int H){       // Find the LCM of L, B, H    int lcm = find_lcm(L, B, H);     // Volume of the cube    int volume_cube = lcm * lcm * lcm;     // Volume of the cuboid    int volume_cuboid = L * B * H;     // Minimum number cuboids required    // to form a cube    System.out.print((volume_cube / volume_cuboid));}static int __gcd(int a, int b) {     return b == 0 ? a:__gcd(b, a % b);    }   // Driver Codepublic static void main(String[] args){       // Given dimensions of cuboid    int L = 1, B = 1, H = 2;     // Function Call    minimumCuboids(L, B, H);}} // This code is contributed by 29AjayKumar
# Python program for the above approach # Function to calculate and# return LCM of a, b, and cdef find_lcm(a, b, c):     # Find GCD of a and b    g = __gcd(a, b);     # Find LCM of a and b    LCM1 = (a * b) // g;     # LCM(a, b, c) = LCM(LCM(a, b), c)    g = __gcd(LCM1, c);     # Finding LCM of a, b, c    LCM = (LCM1 * c) // g;     # return LCM(a, b, c)    return LCM; # Function to find the minimum# number of cuboids required to# make the volume of a valid cubedef minimumCuboids(L, B, H):     # Find the LCM of L, B, H    lcm = find_lcm(L, B, H);     # Volume of the cube    volume_cube = lcm * lcm * lcm;     # Volume of the cuboid    volume_cuboid = L * B * H;     # Minimum number cuboids required    # to form a cube    print((volume_cube // volume_cuboid));def __gcd(a, b):    if(b == 0):        return a;    else:        return __gcd(b, a % b); # Driver Codeif __name__ == '__main__':     # Given dimensions of cuboid    L = 1; B = 1; H = 2;     # Function Call    minimumCuboids(L, B, H); # This code contributed by shikhasingrajput
// C# program for the above approachusing System;class GFG{ // Function to calculate and// return LCM of a, b, and cstatic int find_lcm(int a, int b, int c){       // Find GCD of a and b    int g = __gcd(a, b);     // Find LCM of a and b    int LCM1 = (a * b) / g;     // LCM(a, b, c) = LCM(LCM(a, b), c)    g = __gcd(LCM1, c);     // Finding LCM of a, b, c    int LCM = (LCM1 * c) / g;     // return LCM(a, b, c)    return LCM;} // Function to find the minimum// number of cuboids required to// make the volume of a valid cubestatic void minimumCuboids(int L, int B, int H){       // Find the LCM of L, B, H    int lcm = find_lcm(L, B, H);     // Volume of the cube    int volume_cube = lcm * lcm * lcm;     // Volume of the cuboid    int volume_cuboid = L * B * H;     // Minimum number cuboids required    // to form a cube    Console.Write((volume_cube / volume_cuboid));}static int __gcd(int a, int b) {     return b == 0 ? a:__gcd(b, a % b);    }   // Driver Codepublic static void Main(String[] args){       // Given dimensions of cuboid    int L = 1, B = 1, H = 2;     // Function Call    minimumCuboids(L, B, H);}} // This code is contributed by 29AjayKumar
<script> // Javascript program for the above approach // Function to calculate and// return LCM of a, b, and cfunction find_lcm(a, b, c){       // Find GCD of a and b    let g = __gcd(a, b);     // Find LCM of a and b    let LCM1 = (a * b) / g;     // LCM(a, b, c) = LCM(LCM(a, b), c)    g = __gcd(LCM1, c);     // Finding LCM of a, b, c    let LCM = (LCM1 * c) / g;     // return LCM(a, b, c)    return LCM;} // Function to find the minimum// number of cuboids required to// make the volume of a valid cubefunction minimumCuboids(L, B, H){       // Find the LCM of L, B, H    let lcm = find_lcm(L, B, H);     // Volume of the cube    let volume_cube = lcm * lcm * lcm;     // Volume of the cuboid    let volume_cuboid = L * B * H;     // Minimum number cuboids required    // to form a cube    document.write((volume_cube /                    volume_cuboid));} function __gcd(a, b) {     return b == 0 ? a : __gcd(b, a % b);    } // Driver Code // Given dimensions of cuboidlet L = 1, B = 1, H = 2; // Function CallminimumCuboids(L, B, H); // This code is contributed by splevel62 </script>
4
 
Time Complexity: O(log(min(L, B, H)))Auxiliary Space: O(1)
29AjayKumar
shikhasingrajput
splevel62
simmytarika5
varshagumber28
area-volume-programs
GCD-LCM
LCM
Geometric
Mathematical
Mathematical
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Area of the largest rectangle possible from given coordinates
Total area of two overlapping rectangles
Equation of circle when three points on the circle are given
Count ways to divide circle using N non-intersecting chords
Program to find line passing through 2 Points
Set in C++ Standard Template Library (STL)
Program for Fibonacci numbers
C++ Data Types
Write a program to print all permutations of a given string
Coin Change | DP-7