Given two integers N and K, the task is to find the number of cubes of size K that can be contained in a cube of size N.
Input: N = 2, K = 1
There are 8 cubes of size 1 that can be drawn inside the bigger cube of size 2.
Input: N = 5, K = 2
There are 64 cubes of size 2 can be drawn inside the bigger cube of size 5.
Approach: The key observation to solve the problem is that the number of cubes inside the cube of size N is (N2 * (N+1)2)/4. Therefore, the cubes of size K inside the cube of size N is:
Below is the implementation of the above approach:
Time Complexity: O(1)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.