VOOZH about

URL: https://www.geeksforgeeks.org/dsa/minimum-cuboids-to-form-a-cube/

⇱ Minimum number of Cuboids required to form a Cube - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Minimum number of Cuboids required to form a Cube

Last Updated : 23 Jul, 2025

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:


Output: 
4

 

Time Complexity: O(log(min(L, B, H)))
Auxiliary Space: O(1)

Comment
Article Tags: