Print N numbers such that their product is a Perfect Cube
Given a number N, the task is to find distinct N numbers such that their product is a perfect cube.
Input: N = 3
Output: 1, 8, 27
Product of the output numbers = 1 * 8 * 27 = 216, which is the perfect cube of 6 (63 = 216)
Input: N = 2
Output: 1 8
Product of the output numbers = 1 * 8 = 8, which is the perfect cube of 2 (23 = 8)
Approach: The solution is based on the fact that
The product of the first ‘N’ Perfect Cube numbers is always a Perfect Cube.
So, the Perfect Cube of first N natural numbers will be printed as the output.
For N = 1 =>  Product is 1 and cube root of 1 is also 1 For N = 2 => [1, 8] Product is 8 and cube root of 8 is 2 For N = 3 => [1, 8, 27] Product is 216 and cube root of 216 is 6 and so on
Below is the implementation of the above approach:
1 8 27 64
- Time Complexity: As in the above approach, we are finding the Perfect Cube of N numbers, therefore it will take O(N) time.
- Auxiliary Space Complexity: As in the above approach, there are no extra space used; therefore the Auxiliary Space complexity will be O(1).
My Personal Notes arrow_drop_up
Please Login to comment...