C++ Program to Generate a matrix having sum of secondary diagonal equal to a perfect square Last Updated : 18 Aug, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Given an integer N, the task is to generate a matrix of dimensions N x N using positive integers from the range [1, N] such that the sum of the secondary diagonal is a perfect square. Examples: Input: N = 3Output:1 2 32 3 13 2 1Explanation:The sum of secondary diagonal = 3 + 3 + 3 = 9(= 32). Input: N = 7Output:1 2 3 4 5 6 72 3 4 5 6 7 13 4 5 6 7 1 24 5 6 7 1 2 35 6 7 1 2 3 46 7 1 2 3 4 57 1 2 3 4 5 6Explanation:The sum of secondary diagonal = 7 + 7 + 7 + 7 + 7 + 7 + 7 = 49(= 72). Approach: Since the generated matrix needs to be of dimensions N x N, therefore, to make the sum of elements in the secondary diagonal a perfect square, the idea is to assign N at each index of the secondary diagonal. Therefore, the sum of all N elements in this diagonal is N2, which is a perfect square. Follow the steps below to solve the problem: Initialize a matrix mat[][] of dimension N x N.Initialize the first row of the matrix as {1 2 3 ... N}.Now for the remaining rows of the matrix, fill each row by circular left shift of the arrangement of the previous row of the matrix by 1.Print the matrix after completing the above steps. Below is the implementation of the above approach: C++ // C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to print the matrix whose sum // of element in secondary diagonal is a // perfect square void diagonalSumPerfectSquare(int arr[], int N) { // Iterate for next N - 1 rows for(int i = 0; i < N; i++) { // Print the current row after // the left shift for(int j = 0; j < N; j++) { cout << (arr[(j + i) % 7]) << " "; } cout << endl; } } // Driver Code int main() { // Given N int N = 7; int arr[N]; // Fill the array with elements // ranging from 1 to N for(int i = 0; i < N; i++) { arr[i] = i + 1; } // Function Call diagonalSumPerfectSquare(arr, N); } // This code is contributed by gauravrajput1 Output1 2 3 4 5 6 7 2 3 4 5 6 7 1 3 4 5 6 7 1 2 4 5 6 7 1 2 3 5 6 7 1 2 3 4 6 7 1 2 3 4 5 7 1 2 3 4 5 6 Time Complexity: O(N2)Auxiliary Space: O(N) Please refer complete article on Generate a matrix having sum of secondary diagonal equal to a perfect square for more details! Comment More infoAdvertise with us Next Article C++ Program to Generate a matrix having sum of secondary diagonal equal to a perfect square K kartik Follow Improve Article Tags : C++ rotation array-rearrange maths-perfect-square Practice Tags : CPP Similar Reads Generate a matrix having sum of secondary diagonal equal to a perfect square Given an integer N, the task is to generate a matrix of dimensions N x N using positive integers from the range [1, N] such that the sum of the secondary diagonal is a perfect square. Examples: Input: N = 3Output:1 2 32 3 13 2 1Explanation:The sum of secondary diagonal = 3 + 3 + 3 = 9(= 32). Input: 6 min read Generate a matrix having even sum of all diagonals in each 2 x 2 submatrices Given a positive integer N, the task is to construct a matrix of size N * N such that all the matrix elements are distinct from the range [1, N2] and the sum of elements in both the diagonals of every 2 * 2 submatrices is even. Examples: Input: N = 3 Output: 1 2 3 4 5 6 7 8 9 Explanation: Diagonal e 7 min read Count ways to represent a number as sum of perfect squares Given an integer N, the task is to find the number of ways to represent the number N as sum of perfect squares. Examples: Input: N = 9Output: 4Explanation:There are four ways to represent 9 as the sum of perfect squares:1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 = 91 + 1 + 1 + 1 + 1 + 4 = 91 + 4 + 4 = 99 = 9 13 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 Permutation of numbers such that sum of two consecutive numbers is a perfect square Prerequisite: Hamiltonian Cycle Given an integer n(>=2), find a permutation of numbers from 1 to n such that the sum of two consecutive numbers of that permutation is a perfect square. If that kind of permutation is not possible to print "No Solution". Examples: Input : 17 Output : [16, 9, 7, 2, 15+ min read Like