Given a number **N**, the task is to create a square matrix of size **N*N **with values in range [1, N*N], such that the sum of each diagonal of an even sub-square matrix is even.

**Examples:**

Input:N = 3Output:

1 2 3

4 5 6

7 8 9Explanation:For each even sub-square matrix the sum of each diagonal is a even number.

1 2

4 5

sum of each diagonal is 6 and 6 i.e even number.

Input:N = 4Output:

1 2 3 4

6 5 8 7

9 10 11 12

14 13 16 15Explanation:

For each even sub-square matrix the sum of each diagonal is a even number.

1 2

6 5

sum of each diagonal is 6 and 8 i.e even number.

**Approach:** The idea is to arrange elements from **1 to N*N** in the below-given ways:

- Initialize odd and even by 1 and 2 elements respectively.
- Iterate two nested loop in the range
**[0, N]**. - If the sum of indices in the two nested loops is even the print the value of
**odd**and increment**odd**by**2**and if the sum is odd then print the value of**even**, and increment**even**by**2**.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function to print N*N order matrix ` `// with all sub-matrix of even order ` `// is sum of its diagonal also even ` `void` `evenSubMatrix(` `int` `N) ` `{ ` ` ` `// Even index ` ` ` `int` `even = 1; ` ` ` ` ` `// Odd index ` ` ` `int` `odd = 2; ` ` ` ` ` `// Iterate two nested loop ` ` ` `for` `(` `int` `i = 0; i < N; i++) { ` ` ` ` ` `for` `(` `int` `j = 0; j < N; j++) { ` ` ` ` ` `// For even index the element ` ` ` `// should be consecutive odd ` ` ` `if` `((i + j) % 2 == 0) { ` ` ` `cout << even << ` `" "` `; ` ` ` `even += 2; ` ` ` `} ` ` ` ` ` `// for odd index the element ` ` ` `// should be consecutive even ` ` ` `else` `{ ` ` ` `cout << odd << ` `" "` `; ` ` ` `odd += 2; ` ` ` `} ` ` ` `} ` ` ` `cout << ` `"\n"` `; ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Given order of matrix ` ` ` `int` `N = 4; ` ` ` ` ` `// Function call ` ` ` `evenSubMatrix(N); ` ` ` `return` `0; ` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to print N*N order matrix ` `// with all sub-matrix of even order ` `// is sum of its diagonal also even ` `static` `void` `evenSubMatrix(` `int` `N) ` `{ ` ` ` ` ` `// Even index ` ` ` `int` `even = ` `1` `; ` ` ` ` ` `// Odd index ` ` ` `int` `odd = ` `2` `; ` ` ` ` ` `// Iterate two nested loop ` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++) ` ` ` `{ ` ` ` `for` `(` `int` `j = ` `0` `; j < N; j++) ` ` ` `{ ` ` ` ` ` `// For even index the element ` ` ` `// should be consecutive odd ` ` ` `if` `((i + j) % ` `2` `== ` `0` `) ` ` ` `{ ` ` ` `System.out.print(even + ` `" "` `); ` ` ` `even += ` `2` `; ` ` ` `} ` ` ` ` ` `// For odd index the element ` ` ` `// should be consecutive even ` ` ` `else` ` ` `{ ` ` ` `System.out.print(odd + ` `" "` `); ` ` ` `odd += ` `2` `; ` ` ` `} ` ` ` `} ` ` ` `System.out.println(); ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` ` ` `// Given order of matrix ` ` ` `int` `N = ` `4` `; ` ` ` ` ` `// Function call ` ` ` `evenSubMatrix(N); ` `} ` `} ` ` ` `// This code is contributed by offbeat ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach ` ` ` `# Function to prN*N order matrix ` `# with all sub-matrix of even order ` `# is sum of its diagonal also even ` `def` `evenSubMatrix(N): ` ` ` ` ` `# Even index ` ` ` `even ` `=` `1` ` ` ` ` `# Odd index ` ` ` `odd ` `=` `2` ` ` ` ` `# Iterate two nested loop ` ` ` `for` `i ` `in` `range` `(N): ` ` ` `for` `j ` `in` `range` `(N): ` ` ` ` ` `# For even index the element ` ` ` `# should be consecutive odd ` ` ` `if` `((i ` `+` `j) ` `%` `2` `=` `=` `0` `): ` ` ` `print` `(even, end ` `=` `" "` `) ` ` ` `even ` `+` `=` `2` ` ` ` ` `# For odd index the element ` ` ` `# should be consecutive even ` ` ` `else` `: ` ` ` `print` `(odd, end ` `=` `" "` `) ` ` ` `odd ` `+` `=` `2` ` ` ` ` `print` `() ` ` ` `# Driver Code ` ` ` `# Given order of matrix ` `N ` `=` `4` ` ` `# Function call ` `evenSubMatrix(N) ` ` ` `# This code is contributed by sanjoy_62 ` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to print N*N order matrix ` `// with all sub-matrix of even order ` `// is sum of its diagonal also even ` `static` `void` `evenSubMatrix(` `int` `N) ` `{ ` ` ` ` ` `// Even index ` ` ` `int` `even = 1; ` ` ` ` ` `// Odd index ` ` ` `int` `odd = 2; ` ` ` ` ` `// Iterate two nested loop ` ` ` `for` `(` `int` `i = 0; i < N; i++) ` ` ` `{ ` ` ` `for` `(` `int` `j = 0; j < N; j++) ` ` ` `{ ` ` ` ` ` `// For even index the element ` ` ` `// should be consecutive odd ` ` ` `if` `((i + j) % 2 == 0) ` ` ` `{ ` ` ` `Console.Write(even + ` `" "` `); ` ` ` `even += 2; ` ` ` `} ` ` ` ` ` `// For odd index the element ` ` ` `// should be consecutive even ` ` ` `else` ` ` `{ ` ` ` `Console.Write(odd + ` `" "` `); ` ` ` `odd += 2; ` ` ` `} ` ` ` `} ` ` ` `Console.WriteLine(); ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` ` ` `// Given order of matrix ` ` ` `int` `N = 4; ` ` ` ` ` `// Function call ` ` ` `evenSubMatrix(N); ` `} ` `} ` ` ` `// This code is contributed by amal kumar choubey ` |

*chevron_right*

*filter_none*

**Output:**

1 2 3 4 6 5 8 7 9 10 11 12 14 13 16 15

**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.

## Recommended Posts:

- Construct a Matrix whose sum of diagonals for each square submatrix is even
- Program to calculate area and perimeter of a rhombus whose diagonals are given
- Sum of both diagonals of a spiral odd-order square matrix
- Sum of all parts of a square Matrix divided by its diagonals
- Find the product of sum of two diagonals of a square Matrix
- Largest rectangular sub-matrix whose sum is 0
- Find the largest area rectangular sub-matrix whose sum is equal to k
- Program to find the Sum of each Row and each Column of a Matrix
- Program to Interchange Diagonals of Matrix
- Efficiently compute sums of diagonals of a matrix
- Row-wise common elements in two diagonals of a square matrix
- Swap major and minor diagonals of a square matrix
- Center element of matrix equals sums of half diagonals
- Number of cells in the right and left diagonals passing through (x, y) in a matrix
- Find smallest and largest element from square matrix diagonals
- Program to print the Diagonals of a Matrix
- Finding the converging element of the diagonals in a square matrix
- Program to print the Diagonals of a Matrix in O(N) time
- Return an array of anti-diagonals of given N*N square matrix
- Find the permutation of first N natural numbers such that sum of i % P
_{i}is maximum possible

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.