# Create matrix whose sum of diagonals in each sub matrix is even

• Difficulty Level : Expert
• Last Updated : 10 Feb, 2022

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 = 3
Output:
1 2 3
4 5 6
7 8 9
Explanation: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 = 4
Output:
1 2 3 4
6 5 8 7
9 10 11 12
14 13 16 15
Explanation:
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:

1. Initialize odd and even by 1 and 2 elements respectively.
2. Iterate two nested loop in the range [0, N].
3. 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 ``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;``}`

## 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`

## Python3

 `# Python3 program for the above approach` `# Function to print N*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`

## 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`

## Javascript

 ``

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)

My Personal Notes arrow_drop_up
{"ft_lang_label":"__label__en","ft_lang_prob":0.67736626,"math_prob":0.98103005,"size":4825,"snap":"2022-27-2022-33","text_gpt3_token_len":1537,"char_repetition_ratio":0.12943372,"word_repetition_ratio":0.47664443,"special_character_ratio":0.36,"punctuation_ratio":0.1084463,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99981123,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-08-14T07:14:12Z\",\"WARC-Record-ID\":\"<urn:uuid:44c4c97b-75d9-43cd-b712-4b1d6014fb94>\",\"Content-Length\":\"172250\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:3e8f8fe5-ffe3-4662-8668-c09035dbcb5c>\",\"WARC-Concurrent-To\":\"<urn:uuid:f2bef95c-cae2-4488-bfa4-08a6dc6cb62a>\",\"WARC-IP-Address\":\"23.205.105.172\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/create-matrix-whose-sum-of-diagonals-in-each-sub-matrix-is-even/?ref=rp\",\"WARC-Payload-Digest\":\"sha1:ZFO2EVOFQ3DZNGSXNQJ6YWMYI7TQECSQ\",\"WARC-Block-Digest\":\"sha1:NKOF4DUFGVDXFYIONH5FMAAGMOI6MM5N\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-33/CC-MAIN-2022-33_segments_1659882571996.63_warc_CC-MAIN-20220814052950-20220814082950-00442.warc.gz\"}"}