# Generate a matrix having even sum of all diagonals in each 2 x 2 submatrices

• Last Updated : 10 Nov, 2021

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 elements of every 2 * 2 matrices in the output matrix are { {1, 5}, {2, 4}, {2, 6}, {3, 5}, {4, 8}, {5, 7}, {5, 9}, {6, 8} }. It can be observed that the sum of every diagonal is even.

Input: N = 4
Output:
1 2 3 4
6 5 8 7
9 10 11 12
14 13 16 15

Approach: Follow the steps below to solve the problem:

• Initialize a matrix, say mat[][], to store the matrix elements such that all the matrix elements are distinct from the range [1, N2] and the sum of matrix elements in both the diagonals of every 2 * 2 submatrices is even.
• Initialize a variable, say odd = 1, to store odd numbers.
• Initialize a variable, say even = 2, to store even numbers.
• Fill all the matrix elements, mat[i][j], by checking the following conditions:
• If (i + j) % 2 = 0, then set mat[i][j] = odd and update odd += 2.
• Otherwise, set mat[i][j] = even and update even += 2.
• Finally, print the matrix mat[][].

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach`   `#include ` `using` `namespace` `std;`   `// Function to construct a matrix such that` `// the sum elements in both diagonals of` `// every 2 * 2 matrices is even` `void` `generateMatrix(``int` `N)` `{` `    ``// Stores odd numbers` `    ``int` `odd = 1;`   `    ``// Stores even numbers` `    ``int` `even = 2;`   `    ``// Store matrix elements such that` `    ``// sum of elements in both diagonals` `    ``// of every 2 * 2 submatrices is even` `    ``int` `mat[N + 1][N + 1];`   `    ``// Fill all the values of` `    ``// matrix elements` `    ``for` `(``int` `i = 1; i <= N; i++) {`   `        ``for` `(``int` `j = 1; j <= N; j++) {`   `            ``if` `((i + j) % 2 == 0) {`   `                ``mat[i][j] = odd;`   `                ``// Update odd` `                ``odd += 2;` `            ``}`   `            ``else` `{`   `                ``mat[i][j] = even;`   `                ``// Update even` `                ``even += 2;` `            ``}` `        ``}` `    ``}`   `    ``// Print the matrix` `    ``for` `(``int` `i = 1; i <= N; i++) {`   `        ``for` `(``int` `j = 1; j <= N; j++) {`   `            ``cout << mat[i][j] << ``" "``;` `        ``}`   `        ``cout << endl;` `    ``}` `}`   `// Driver Code` `int` `main()` `{` `    ``int` `N = 4;` `    ``generateMatrix(N);`   `    ``return` `0;` `}`

## Java

 `// Java program for the above approach` `import` `java.io.*;`   `class` `GFG{`   `// Function to construct a matrix such that` `// the sum elements in both diagonals of` `// every 2 * 2 matrices is even` `static` `void` `generateMatrix(``int` `N)` `{` `    `  `    ``// Stores odd numbers` `    ``int` `odd = ``1``;`   `    ``// Stores even numbers` `    ``int` `even = ``2``;`   `    ``// Store matrix elements such that` `    ``// sum of elements in both diagonals` `    ``// of every 2 * 2 submatrices is even` `    ``int``[][] mat = ``new` `int``[N + ``1``][N + ``1``];`   `    ``// Fill all the values of` `    ``// matrix elements` `    ``for``(``int` `i = ``1``; i <= N; i++) ` `    ``{` `        ``for``(``int` `j = ``1``; j <= N; j++) ` `        ``{` `            ``if` `((i + j) % ``2` `== ``0``) ` `            ``{` `                ``mat[i][j] = odd;` `                `  `                ``// Update odd` `                ``odd += ``2``;` `            ``}`   `            ``else` `            ``{` `                ``mat[i][j] = even;` `                `  `                ``// Update even` `                ``even += ``2``;` `            ``}` `        ``}` `    ``}`   `    ``// Print the matrix` `    ``for``(``int` `i = ``1``; i <= N; i++) ` `    ``{` `        ``for``(``int` `j = ``1``; j <= N; j++) ` `        ``{` `            ``System.out.print(mat[i][j] + ``" "``);` `        ``}` `        `  `        ``System.out.println();` `    ``}` `}`   `// Driver Code` `public` `static` `void` `main(String[] args)` `{` `    ``int` `N = ``4``;` `    `  `    ``generateMatrix(N);` `}` `}`   `// This code is contributed by Dharanendra L V`

## Python3

 `# Python program for the above approach`   `# Function to construct a matrix such that` `# the sum elements in both diagonals of` `# every 2 * 2 matrices is even` `def` `generateMatrix(N):` `  `  `    ``# Stores odd numbers` `    ``odd ``=` `1``;`   `    ``# Stores even numbers` `    ``even ``=` `2``;`   `    ``# Store matrix elements such that` `    ``# sum of elements in both diagonals` `    ``# of every 2 * 2 submatrices is even` `    ``mat ``=` `[[``0` `for` `i ``in` `range``(N ``+` `1``)] ``for` `j ``in` `range``(N ``+` `1``)] ;`   `    ``# Fill all the values of` `    ``# matrix elements` `    ``for` `i ``in` `range``(``1``, N ``+` `1``):` `        ``for` `j ``in` `range``(``1``, N ``+` `1``):` `            ``if` `((i ``+` `j) ``%` `2` `=``=` `0``):` `                ``mat[i][j] ``=` `odd;`   `                ``# Update odd` `                ``odd ``+``=` `2``;` `            ``else``:` `                ``mat[i][j] ``=` `even;`   `                ``# Update even` `                ``even ``+``=` `2``;`   `    ``# Print the matrix` `    ``for` `i ``in` `range``(``1``, N ``+` `1``):` `        ``for` `j ``in` `range``(``1``, N ``+` `1``):` `            ``print``(mat[i][j], end ``=` `" "``);` `        ``print``();`   `# Driver Code` `if` `__name__ ``=``=` `'__main__'``:` `    ``N ``=` `4``;` `    ``generateMatrix(N);`   `# This code is contributed by 29AjayKumar`

## C#

 `// C# program for the above approach` `using` `System;`   `class` `GFG{`   `// Function to construct a matrix such that` `// the sum elements in both diagonals of` `// every 2 * 2 matrices is even` `static` `void` `generateMatrix(``int` `N)` `{` `    `  `    ``// Stores odd numbers` `    ``int` `odd = 1;`   `    ``// Stores even numbers` `    ``int` `even = 2;`   `    ``// Store matrix elements such that` `    ``// sum of elements in both diagonals` `    ``// of every 2 * 2 submatrices is even` `    ``int``[,] mat = ``new` `int``[N + 1, N + 1];`   `    ``// Fill all the values of` `    ``// matrix elements` `    ``for``(``int` `i = 1; i <= N; i++) ` `    ``{` `        ``for``(``int` `j = 1; j <= N; j++) ` `        ``{` `            ``if` `((i + j) % 2 == 0) ` `            ``{` `                ``mat[i, j] = odd;` `                `  `                ``// Update odd` `                ``odd += 2;` `            ``}`   `            ``else` `            ``{` `                ``mat[i, j] = even;`   `                ``// Update even` `                ``even += 2;` `            ``}` `        ``}` `    ``}`   `    ``// Print the matrix` `    ``for``(``int` `i = 1; i <= N; i++) ` `    ``{` `        ``for``(``int` `j = 1; j <= N; j++) ` `        ``{` `            ``Console.Write(mat[i, j] + ``" "``);` `        ``}`   `        ``Console.WriteLine();` `    ``}` `}`   `// Driver Code` `static` `public` `void` `Main()` `{` `    ``int` `N = 4;` `    `  `    ``generateMatrix(N);` `}` `}`   `// This code is contributed by Dharanendra L V`

## Javascript

 ``

Output:

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

Time Complexity: O(N2)
Auxiliary Space: O(N2)

My Personal Notes arrow_drop_up
Recommended Articles
Page :