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

9 10 11 12

16 15 14 13Explanation:

Sum of diagonal = 32 and 36 and integer N = 4, all the numbers are even that is same parity.

Input:N = 3Output:

1 2 3

6 5 4

7 8 9Explanation:

Sum of diagonal = 15 and integer N = 3, all the numbers are odd that is same parity.

**Approach:** The idea is to observe that on filling the elements in the matrix in an **alternative fashion** the parity of **N** and the sum of diagonals is the same. Start the counter from 1 and then fill the first row from **0 to N – 1** in increasing order, then fill the second row from index **N – 1 to 0**, and so on. Keep filling each element from value **1 to N ^{2}** in this alternate fashion to get the required matrix.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include<bits/stdc++.h>` `using` `namespace` `std;` `// Function to construct a N * N` `// matrix based on the given condition` `void` `createMatrix(` `int` `N)` `{` ` ` `// Matrix with given sizM` ` ` `int` `M[N][N];` ` ` `// Counter to insert elements` ` ` `// from 1 to N * N` ` ` `int` `count = 1;` ` ` `for` `(` `int` `i = 0; i < N; i++) {` ` ` `// Check if it is first row` ` ` `// or odd row of the matrix` ` ` `if` `(i % 2 == 0) {` ` ` `for` `(` `int` `j = 0; j < N; j++) {` ` ` `M[i][j] = count;` ` ` `count++;` ` ` `}` ` ` `}` ` ` `else` ` ` `{` ` ` `// Insert elements from` ` ` `// right to left` ` ` `for` `(` `int` `j = N - 1;j >= 0; j--){` ` ` `M[i][j] = count;` ` ` `count += 1;` ` ` `}` ` ` `}` ` ` `}` ` ` `// Print the matrix` ` ` `for` `(` `int` `i = 0; i < N; i++) {` ` ` `// Traverse column` ` ` `for` `(` `int` `j = 0; j < N; j++) {` ` ` `cout << M[i][j] << ` `" "` `;` ` ` `}` ` ` `cout << endl;` ` ` `}` `}` `// Driver Code` `int` `main()` `{` ` ` `// Given size of matrix N` ` ` `int` `N = 3;` ` ` `// Function Call` ` ` `createMatrix(N);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach` `class` `GFG{` ` ` `// Function to construct a N * N` `// matrix based on the given condition` `static` `void` `createMatrix(` `int` `N)` `{` ` ` `// Matrix with given sizM` ` ` `int` `M[][] = ` `new` `int` `[N][N];` ` ` ` ` `// Counter to insert elements` ` ` `// from 1 to N * N` ` ` `int` `count = ` `1` `;` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++) ` ` ` `{` ` ` ` ` `// Check if it is first row` ` ` `// or odd row of the matrix` ` ` `if` `(i % ` `2` `== ` `0` `)` ` ` `{` ` ` ` ` `for` `(` `int` `j = ` `0` `; j < N; j++)` ` ` `{` ` ` `M[i][j] = count;` ` ` `count++;` ` ` `}` ` ` `}` ` ` `else` ` ` `{` ` ` `// Insert elements from` ` ` `// right to left` ` ` `for` `(` `int` `j = N - ` `1` `; j >= ` `0` `; j--){` ` ` `M[i][j] = count;` ` ` `count += ` `1` `;` ` ` `}` ` ` `}` ` ` `}` ` ` ` ` `// Print the matrix` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++) ` ` ` `{` ` ` ` ` `// Traverse column` ` ` `for` `(` `int` `j = ` `0` `; j < N; j++)` ` ` `{` ` ` `System.out.print(M[i][j] + ` `" "` `);` ` ` `}` ` ` `System.out.println();` ` ` `}` `}` ` ` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `// Given size of matrix N` ` ` `int` `N = ` `3` `;` ` ` ` ` `// Function Call` ` ` `createMatrix(N);` `}` `}` `// This code is contributed by Ritik Bansal` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach` `# Function to construct a N * N` `# matrix based on the given condition` `def` `createMatrix(N):` ` ` ` ` `# Matrix with given size` ` ` `M ` `=` `[[` `0` `] ` `*` `N ` `for` `i ` `in` `range` `(N)]` ` ` `# Counter to insert elements` ` ` `# from 1 to N * N` ` ` `count ` `=` `1` ` ` ` ` `for` `i ` `in` `range` `(N):` ` ` `# Check if it is first row` ` ` `# or odd row of the matrix` ` ` `if` `(i ` `%` `2` `=` `=` `0` `):` ` ` `for` `j ` `in` `range` `(N):` ` ` `# Insert elements from` ` ` `# left to right` ` ` `M[i][j] ` `=` `count` ` ` `count ` `+` `=` `1` ` ` `# Condition if it is second` ` ` `# row or even row` ` ` `else` `:` ` ` `# Insert elements from` ` ` `# right to left` ` ` `for` `j ` `in` `range` `(N ` `-` `1` `, ` `-` `1` `, ` `-` `1` `):` ` ` `M[i][j] ` `=` `count` ` ` `count ` `+` `=` `1` ` ` `# Print the matrix` ` ` `for` `i ` `in` `range` `(N):` ` ` `# Traverse column` ` ` `for` `j ` `in` `range` `(N):` ` ` `print` `(M[i][j], end ` `=` `" "` `)` ` ` `print` `()` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` ` ` `# Given size of matrix N` ` ` `N ` `=` `3` ` ` `# Function call` ` ` `createMatrix(N)` `# This code is contributed by mohit kumar 29` |

*chevron_right*

*filter_none*

## C#

`// C# program for ` `// the above approach` `using` `System;` `class` `GFG{` ` ` `// Function to construct a N * N` `// matrix based on the given condition` `static` `void` `createMatrix(` `int` `N)` `{` ` ` `// Matrix with given sizM` ` ` `int` `[,] M = ` `new` `int` `[N, N];` ` ` `// Counter to insert elements` ` ` `// from 1 to N * N` ` ` `int` `count = 1;` ` ` `for` `(` `int` `i = 0; i < N; i++) ` ` ` `{` ` ` `// Check if it is first row` ` ` `// or odd row of the matrix` ` ` `if` `(i % 2 == 0)` ` ` `{` ` ` `for` `(` `int` `j = 0; j < N; j++) ` ` ` `{` ` ` `M[i, j] = count;` ` ` `count++;` ` ` `}` ` ` `}` ` ` `else` ` ` `{` ` ` `// Insert elements from` ` ` `// right to left` ` ` `for` `(` `int` `j = N - 1; j >= 0; j--)` ` ` `{` ` ` `M[i, j] = count;` ` ` `count += 1;` ` ` `}` ` ` `}` ` ` `}` ` ` `// Print the matrix` ` ` `for` `(` `int` `i = 0; i < N; i++) ` ` ` `{` ` ` `// Traverse column` ` ` `for` `(` `int` `j = 0; j < N; j++)` ` ` `{` ` ` `Console.Write(M[i, j] + ` `" "` `);` ` ` `}` ` ` `Console.WriteLine();` ` ` `}` `}` ` ` `// Driver Code` `public` `static` `void` `Main()` `{` ` ` `// Given size of matrix N` ` ` `int` `N = 3;` ` ` `// Function Call` ` ` `createMatrix(N);` `}` `}` `// This code is contributed by Chitranayal` |

*chevron_right*

*filter_none*

**Output:**

1 2 3 6 5 4 7 8 9

**Time Complexity:** O(N*N) **Auxiliary 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.