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.

**Examples:**

Input:N = 5

Output:1 2 3 4 5

Explanation

Sum of cube of all elements = 1 + 8 + 27 + 64 + 125 = 225

which is a perfect square number.

Input:N = 1

Output:1

**Solution Approach:**

- 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:

## C++

`// C++ implementation of the ` `// above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to construct an array ` `// of size N ` `void` `constructArray(` `int` `N) ` `{ ` ` ` `for` `(` `int` `i = 1; i <= N; i++) { ` ` ` ` ` `// Prints the first N ` ` ` `// natural numbers ` ` ` `cout << i << ` `" "` `; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 5; ` ` ` `constructArray(N); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the ` `// above approach ` `class` `GFG{ ` ` ` `// Function to construct an array ` `// of size N ` `public` `static` `void` `constructArray(` `int` `N) ` `{ ` ` ` `for` `(` `int` `i = ` `1` `; i <= N; i++) ` ` ` `{ ` ` ` ` ` `// Prints the first N ` ` ` `// natural numbers ` ` ` `System.out.print(i + ` `" "` `); ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `5` `; ` ` ` `constructArray(N); ` `} ` `} ` ` ` `// This code is contributed by divyeshrabadiya07 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the ` `# above approach ` ` ` `# Function to construct an array ` `# of size N ` `def` `constructArray(N): ` ` ` ` ` `for` `i ` `in` `range` `(` `1` `, N ` `+` `1` `): ` ` ` ` ` `# Prints the first N ` ` ` `# natural numbers ` ` ` `print` `(i, end ` `=` `' '` `) ` ` ` ` ` `# Driver code ` `if` `__name__` `=` `=` `'__main__'` `: ` ` ` ` ` `N ` `=` `5` ` ` ` ` `constructArray(N) ` ` ` `# This code is contributed by rutvik_56 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the ` `// above approach ` `using` `System; ` `class` `GFG{ ` ` ` `// Function to construct an array ` `// of size N ` `public` `static` `void` `constructArray(` `int` `N) ` `{ ` ` ` `for` `(` `int` `i = 1; i <= N; i++) ` ` ` `{ ` ` ` ` ` `// Prints the first N ` ` ` `// natural numbers ` ` ` `Console.Write(i + ` `" "` `); ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `N = 5; ` ` ` `constructArray(N); ` `} ` `} ` ` ` `// This code is contributed by sapnasingh4991 ` |

*chevron_right*

*filter_none*

**Output:**

1 2 3 4 5

**Time Complexity:** O(N)

**Auxillary 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.

## Recommended Posts:

- 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
- 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
- Smallest perfect square divisible by all elements of an array
- Construct a distinct elements array with given size, sum and element upper bound
- Count of elements to be multiplied with integers to make each pair of Array a perfect square
- Array range queries to find the number of perfect square elements with updates
- Previous perfect square and cube number smaller than number N
- Smallest perfect cube in an array
- Largest number in an array that is not a perfect cube
- Largest perfect cube number in an Array
- Count of pairs in an Array whose sum is a Perfect Cube
- Largest perfect square number in an Array
- Maximum subsequence sum from a given array which is a perfect square
- Count of pairs in an array whose sum is a perfect square
- Count of pairs in an array whose product is a perfect square
- Check if product of array containing prime numbers is a perfect square
- Find smallest perfect square number A such that N + A is also a perfect square number
- Smallest N digit number whose sum of square of digits is a Perfect Square

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.