![]() |
VOOZH | about |
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:
Below is the implementation of the above approach:
4
Time Complexity: O(log(min(L, B, H)))
Auxiliary Space: O(1)