Construct an Array of size N whose sum of cube of all elements is a perfect square Last Updated : 15 Jul, 2025 Comments Improve Suggest changes Like Article Like Report 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. \sum_{}A_i^3=X^2 , 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:(\frac{N \times(N+1)}{2})^2 So, the summation is itself, a perfect square of the integer X^2&=(\frac{N \times(N+1)}{2})^2 Therefore X&=(\frac{N \times(N+1)}{2}) , 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; } Java // Java implementation of the // above approach import java.io.*; public 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 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 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 JavaScript <script> // JavaScript implementation of the // above approach // Function to construct an array // of size N function constructArray(N) { for(let i = 1; i <= N; i++) { // Prints the first N // natural numbers document.write(i + " "); } } // Driver code let N = 5; constructArray(N); // This code is contributed by Surbhi Tyagi. </script> Output: 1 2 3 4 5 Time Complexity: O(N) Auxiliary Space: O(1) Create Quiz Comment K koulick_sadhu Follow 0 Improve K koulick_sadhu Follow 0 Improve Article Tags : DSA maths-perfect-square maths-cube Natural Numbers Explore DSA FundamentalsLogic Building Problems 2 min read Analysis of Algorithms 1 min read Data StructuresArray Data Structure 3 min read String in Data Structure 2 min read Hashing in Data Structure 2 min read Linked List Data Structure 2 min read Stack Data Structure 2 min read Queue Data Structure 2 min read Tree Data Structure 2 min read Graph Data Structure 3 min read Trie Data Structure 15+ min read AlgorithmsSearching Algorithms 2 min read Sorting Algorithms 3 min read Introduction to Recursion 15 min read Greedy Algorithms 3 min read Graph Algorithms 3 min read Dynamic Programming or DP 3 min read Bitwise Algorithms 4 min read AdvancedSegment Tree 2 min read Binary Indexed Tree or Fenwick Tree 15 min read Square Root (Sqrt) Decomposition Algorithm 15+ min read Binary Lifting 15+ min read Geometry 2 min read Interview PreparationInterview Corner 3 min read GfG160 3 min read Practice ProblemGeeksforGeeks Practice - Leading Online Coding Platform 1 min read Problem of The Day - Develop the Habit of Coding 5 min read Like