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

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:
- 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 evenvoid 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 Codeint main(){    // Given order of matrix    int N = 4;Â
    // Function call    evenSubMatrix(N);    return 0;} |
Java
// Java program for the above approachimport java.util.*;Â
class GFG{Â
// Function to print N*N order matrix// with all sub-matrix of even order// is sum of its diagonal also evenstatic 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 codepublic 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 evendef 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 matrixN = 4Â
# Function callevenSubMatrix(N)Â
# This code is contributed by sanjoy_62 |
C#
// C# program for the above approachusing System;Â
class GFG{Â
// Function to print N*N order matrix// with all sub-matrix of even order// is sum of its diagonal also evenstatic 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 codepublic 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
<script>// Java script 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 evenfunction evenSubMatrix( N){         // Even index    let even = 1;Â
    // Odd index    let odd = 2;Â
    // Iterate two nested loop    for(let i = 0; i < N; i++)    {        for(let j = 0; j < N; j++)        {                         // For even index the element            // should be consecutive odd            if ((i + j) % 2 == 0)            {                document.write(even + " ");                even += 2;            }                         // For odd index the element            // should be consecutive even            else            {                document.write(odd + " ");                odd += 2;            }        }        document.write("<br>");    }}Â
// Driver codeÂ
         // Given order of matrix    let N = 4;         // Function call    evenSubMatrix(N);Â
// This code is contributed by manoj</script> |
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)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



