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++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to calculate and // return LCM of a, b, and c 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 cube 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 cout << (volume_cube / volume_cuboid); } // Driver Code int main() { // Given dimensions of cuboid int L = 1, B = 1, H = 2; // Function Call minimumCuboids(L, B, H); return 0; } |
Java
// Java program for the above approach import java.util.*; class GFG { // Function to calculate and // return LCM of a, b, and c static 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 cube static 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 Code public 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 |
Python3
# Python program for the above approach # Function to calculate and # return LCM of a, b, and c def 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 cube def 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 Code if __name__ = = '__main__' : # Given dimensions of cuboid L = 1 ; B = 1 ; H = 2 ; # Function Call minimumCuboids(L, B, H); # This code contributed by shikhasingrajput |
C#
// C# program for the above approach using System; class GFG { // Function to calculate and // return LCM of a, b, and c static 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 cube static 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 Code public 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 |
Javascript
<script> // Javascript program for the above approach // Function to calculate and // return LCM of a, b, and c function 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 cube function 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 cuboid let L = 1, B = 1, H = 2; // Function Call minimumCuboids(L, B, H); // This code is contributed by splevel62 </script> |
4
Time Complexity: O(log(min(L, B, H)))
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!