Crear array cuya suma de diagonales en cada subarray sea par

Dado un número N , la tarea es crear una array cuadrada de tamaño N*N con valores en el rango [1, N*N], tal que la suma de cada diagonal de una array subcuadrada par sea par.

Ejemplos:

Entrada: N = 3
Salida:  
1 2 3 
4 5 6 
7 8 9 
Explicación: Para cada array subcuadrada par, la suma de cada diagonal es un número par.
1 2 
4 5 
la suma de cada diagonal es 6 y 6, es decir, un número par.

Entrada: N = 4
Salida: 
1 2 3 4 
6 5 8 7 
9 10 11 12 
14 13 16 15 
Explicación: 
Para cada array subcuadrada par, la suma de cada diagonal es un número par.
1 2 
6 5 
la suma de cada diagonal es 6 y 8, es decir, un número par.

Enfoque: La idea es organizar elementos de 1 a N*N de las siguientes maneras:

  1. Inicialice pares e impares por 1 y 2 elementos respectivamente.
  2. Iterar dos bucles anidados en el rango [0, N] .
  3. Si la suma de los índices en los dos bucles anidados es par, imprima el valor de impar e incremente impar en 2 y si la suma es impar, imprima el valor de par e incremente par en 2 .

A continuación se muestra la implementación del enfoque anterior:

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;
}

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

<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 even
function 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>
Producción: 

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

Complejidad temporal: O(N*N)
Espacio auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por thakurabhaysingh445 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *