Given the size of array N, the task is to construct an array of size N with positive integer elements such that the cube sum of all elements of this array is a perfect square.
Note: Repititions of integers are allowed.
Input: N = 1
Explanation: Cube of 4 is 64, and 64 is perfect square.
Input: N = 6
Output: 5 10 5 10 5 5
Explanation: Cubic sum of array element is (5)3 + (10)3 + (5)3 + (10)3 + (5)3 + (5)3 = 2500, and 2500 is perfect square.
Approach: The idea is to construct array of size, N which contain first N natural number. If closely observed,
cubic sum of first N natural number: (1)3 + (2)3 + (3)3 + (4)3 + …….(N)3 is always perfect square.
Below is the implementation of the above approach.
1, 2, 3, 4, 5, 6,
Time Complexity: O(N) where N is the size of the Array.
Space Complexity: 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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with industry experts, please refer Geeks Classes Live