Construct an Array of size N whose sum of cube of all elements is a perfect square Last Updated : 20 Dec, 2022 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) Comment More infoAdvertise with us Next Article Construct an Array of size N whose sum of cube of all elements is a perfect square koulick_sadhu Follow Improve Article Tags : Mathematical Competitive Programming Computer Science Fundamentals DSA Arrays maths-perfect-square maths-cube Natural Numbers +4 More Practice Tags : ArraysMathematical Similar Reads Construct an Array such that cube sum of all element is a perfect square 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: Repetitions of integers are allowed.Example: Input: N = 1 Output: 4 Explanation: Cube of 4 is 64, and 64 is perfect squ 4 min read Count of pairs in an array whose sum is a perfect square Given an array arr of distinct elements, the task is to find the total number of two element pairs from the array whose sum is a perfect square. Examples: Input: arr[] = {2, 3, 6, 9, 10, 20} Output: 2 Only possible pairs are (3, 6) and (6, 10) Input: arr[] = {9, 2, 5, 1} Output: 0 Naive Approach: Us 12 min read Count of pairs in an Array whose sum is a Perfect Cube Given an array arr of distinct elements of size N, the task is to find the total number of pairs in the array whose sum is a perfect cube.Examples: Input: arr[] = {2, 3, 6, 9, 10, 20} Output: 1 Only possible pair is (2, 6)Input: arr[] = {9, 2, 5, 1} Output: 0 Naive Approach: Use nested loops and che 15 min read Construct an Array of size N in which sum of odd elements is equal to sum of even elements Given an integer N which is always even, the task is to create an array of size N which contains N/2 even numbers and N/2 odd numbers. All the elements of array should be distinct and the sum of even numbers is equal to the sum of odd numbers. If no such array exists then print -1.Examples: Input: N 7 min read Construct a square Matrix whose parity of diagonal sum is same as size of matrix Given an integer N representing the size of the matrix, the task is to construct a square matrix N * N which have an element from 1 to N2 such that the parity of the sum of its diagonals is equal to the parity of integer N. Examples: Input: N = 4Output:1 2 3 4 8 7 6 59 10 11 1216 15 14 13Explanation 6 min read XOR of all Array elements that are perfect squares Given an array arr[] containing N integers, the task is to find the XOR of all the elements of this array that are perfect squares. Examples: Input: arr[] = {3, 9, 16, 12, 13, 15} Output: 25Explanation: Only 9, 16 are the perfect squares in the given array.So the XOR of 9 and 16 is 25, XOR = 9 ^ 16 5 min read Generate an alternate odd-even sequence having sum of all consecutive pairs as a perfect square Given an integer N, the task is to print a sequence of length N consisting of alternate odd and even numbers in increasing order such that the sum of any two consecutive terms is a perfect square. Examples: Input: N = 4Output: 1 8 17 32Explanation:1 + 8 = 9 = 328 + 17 = 25 = 5217 + 32 = 49 = 72 Inpu 3 min read Count pairs (a, b) whose sum of squares is N (a^2 + b^2 = N) Given a number N, the task is to count all âaâ and âbâ that satisfy the condition a^2 + b^2 = N.Note:- (a, b) and (b, a) are to be considered as two different pairs and (a, a) is also valid and to be considered only one time.Examples: Input: N = 10 Output: 2 1^2 + 3^2 = 10 3^2 + 1^2 = 10 Input: N = 7 min read Find a subarray of size K whose sum is a perfect square Given an array arr[] and an integer K, the task is to find a subarray of length K having a sum which is a perfect square. If no such subarray exists, then print -1. Otherwise, print the subarray. Note: There can be more than one possible subarray. Print any one of them.Examples: Input: arr[] = {20, 15 min read Check if the sum of perfect squares in an array is divisible by x Given an array arr[] and an integer x, the task is to check whether the sum of all the perfect squares from the array is divisible by x or not. If divisible then print Yes else print No. Examples: Input: arr[] = {2, 3, 4, 6, 9, 10}, x = 13 Output: Yes 4 and 9 are the only perfect squares from the ar 6 min read Like