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 3

2 3 1

3 2 1Explanation:

The sum of secondary diagonal = 3 + 3 + 3 = 9(= 3^{2}).

Input:N = 7Output:

1 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 6Explanation:

The sum of secondary diagonal = 7 + 7 + 7 + 7 + 7 + 7 + 7 = 49(= 7^{2}).

**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 **N ^{2}**, 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:

## Java

`// Java program for the above approach` `class` `GFG{` ` ` `// Function to print the matrix whose sum` `// of element in secondary diagonal is a` `// perfect square` `static` `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++) ` ` ` `{` ` ` `System.out.print(arr[(j + i) % ` `7` `] + ` `" "` `);` ` ` `}` ` ` `System.out.println();` ` ` `}` `}` `// Driver Code` `public` `static` `void` `main(String[] srgs)` `{` ` ` ` ` `// Given N` ` ` `int` `N = ` `7` `;` ` ` `int` `[] arr = ` `new` `int` `[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 Amit Katiyar` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach` `# Function to print the matrix whose sum` `# of element in secondary diagonal is a` `# perfect square` `def` `diagonalSumPerfectSquare(arr, N):` ` ` ` ` `# Print the current row` ` ` `print` `(` `*` `arr, sep ` `=` `" "` `)` ` ` ` ` `# Iterate for next N - 1 rows` ` ` `for` `i ` `in` `range` `(N` `-` `1` `):` ` ` ` ` `# Perform left shift by 1` ` ` `arr ` `=` `arr[` `1` `::] ` `+` `arr[:` `1` `:]` ` ` ` ` `# Print the current row after` ` ` `# the left shift` ` ` `print` `(` `*` `arr, sep ` `=` `" "` `)` `# Driver Code` `# Given N` `N ` `=` `7` `arr ` `=` `[]` `# Fill the array with elements` `# ranging from 1 to N` `for` `i ` `in` `range` `(` `1` `, N ` `+` `1` `):` ` ` `arr.append(i)` `# Function Call` `diagonalSumPerfectSquare(arr, N)` |

*chevron_right*

*filter_none*

## C#

`// C# program for the` `// above approach` `using` `System;` `class` `GFG{` ` ` `// Function to print the matrix whose sum` `// of element in secondary diagonal is a` `// perfect square` `static` `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++) ` ` ` `{` ` ` `Console.Write(` ` ` `arr[(j + i) % 7] + ` `" "` `);` ` ` `}` ` ` `Console.WriteLine();` ` ` `}` `}` `// Driver Code` `public` `static` `void` `Main(String[] srgs)` `{ ` ` ` `// Given N` ` ` `int` `N = 7;` ` ` `int` `[] arr = ` `new` `int` `[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 29AjayKumar` |

*chevron_right*

*filter_none*

**Output:**

1 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(N^{2})**Auxiliary Space:** O(N)

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:

- Check if a number is a perfect square having all its digits as a perfect square
- Filling diagonal to make the sum of every row, column and diagonal equal of 3x3 matrix
- Generate an alternate odd-even sequence having sum of all consecutive pairs as a perfect square
- Find smallest perfect square number A such that N + A is also a perfect square number
- Maximum sum of elements in a diagonal parallel to the main diagonal of a given Matrix
- Construct a square Matrix whose parity of diagonal sum is same as size of matrix
- Program to swap upper diagonal elements with lower diagonal elements of matrix.
- Smallest N digit number whose sum of square of digits is a Perfect Square
- Sum of non-diagonal parts of a square Matrix
- Count numbers upto N which are both perfect square and perfect cube
- Construct a matrix with sum equal to the sum of diagonal elements
- Length of a Diagonal of a Parallelogram using the length of Sides and the other Diagonal
- Check if a number is perfect square without finding square root
- Print all the sub diagonal elements of the given square matrix
- Print all the super diagonal elements of the given square matrix
- Count of subarrays having exactly K perfect square numbers
- Introduction of Secondary Memory
- Program to check diagonal matrix and scalar matrix
- Program to convert given Matrix to a Diagonal Matrix
- Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively

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.