Given an integer N, the tasks is to construct a sorted array arr of size N, such that the sum of cube of all elements is a perfect square, i.e. , where X is an integer.
Input: N = 5
Output: 1 2 3 4 5
Sum of cube of all elements = 1 + 8 + 27 + 64 + 125 = 225
which is a perfect square number.
Input: N = 1
- The sum of cubes of first N natural number is given by:
- So, the summation is itself, a perfect square of the integer
- Therefore , which is nothing but sum of N natural numbers.
- So, just print the first N natural numbers to construct the array.
Below is the implementation of the above approach:
1 2 3 4 5
Time Complexity: O(N)
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.
- Construct an Array such that cube sum of all element is a perfect square
- Count numbers upto N which are both perfect square and perfect cube
- Check if a number is a perfect square having all its digits as a perfect square
- Count all triplets whose sum is equal to a perfect cube
- Count of pairs in an Array whose sum is a Perfect Cube
- Smallest N digit number whose sum of square of digits is a Perfect Square
- Construct a square Matrix whose parity of diagonal sum is same as size of matrix
- Find smallest perfect square number A such that N + A is also a perfect square number
- Find a subarray of size K whose sum is a perfect square
- Smallest perfect Cube divisible by all elements of an array
- Construct an Array of size N in which sum of odd elements is equal to sum of even elements
- Previous perfect square and cube number smaller than number N
- Count of pairs in an array whose sum is a perfect square
- Construct a Matrix whose sum of diagonals for each square submatrix is even
- Count of subarrays whose sum is a perfect square
- Count of pairs in an array whose product is a perfect square
- Percentage increase in volume of the cube if a side of cube is increased by a given percentage
- Smallest perfect square divisible by all elements of an array
- Count cubes of size K inscribed in a cube of size N
- Largest sub-array whose all elements are perfect squares
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.