Dado un número entero N que representa el tamaño de la array, la tarea es construir una array cuadrada N * N que tenga un elemento de 1 a N 2 tal que la paridad de la suma de sus diagonales sea igual a la paridad del número entero N.
Ejemplos:
Entrada: N = 4
Salida:
1 2 3 4
8 7 6 5
9 10 11 12
16 15 14 13
Explicación:
Suma de diagonales = 32 y 36 y entero N = 4, todos los números son pares, es decir, la misma paridad.Entrada: N = 3
Salida:
1 2 3
6 5 4
7 8 9
Explicación:
La suma de la diagonal = 15 y el número entero N = 3, todos los números son impares, es decir, la misma paridad.
Planteamiento: La idea es observar que al llenar los elementos de la array de forma alternativa la paridad de N y la suma de las diagonales es la misma. Comience el contador desde 1 y luego complete la primera fila de 0 a N – 1 en orden creciente, luego complete la segunda fila del índice N – 1 a 0 , y así sucesivamente. Siga llenando cada elemento desde el valor 1 hasta el N 2 de esta manera alternativa para obtener la array requerida.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include<bits/stdc++.h> using namespace std; // Function to construct a N * N // matrix based on the given condition void createMatrix(int N) { // Matrix with given sizM int M[N][N]; // Counter to insert elements // from 1 to N * N int count = 1; for (int i = 0; i < N; i++) { // Check if it is first row // or odd row of the matrix if (i % 2 == 0) { for (int j = 0; j < N; j++) { M[i][j] = count; count++; } } else { // Insert elements from // right to left for (int j = N - 1;j >= 0; j--){ M[i][j] = count; count += 1; } } } // Print the matrix for (int i = 0; i < N; i++) { // Traverse column for (int j = 0; j < N; j++) { cout << M[i][j] << " "; } cout << endl; } } // Driver Code int main() { // Given size of matrix N int N = 3; // Function Call createMatrix(N); return 0; }
Java
// Java program for the above approach class GFG{ // Function to construct a N * N // matrix based on the given condition static void createMatrix(int N) { // Matrix with given sizM int M[][] = new int[N][N]; // Counter to insert elements // from 1 to N * N int count = 1; for (int i = 0; i < N; i++) { // Check if it is first row // or odd row of the matrix if (i % 2 == 0) { for (int j = 0; j < N; j++) { M[i][j] = count; count++; } } else { // Insert elements from // right to left for(int j = N - 1; j >= 0; j--){ M[i][j] = count; count += 1 ; } } } // Print the matrix for (int i = 0; i < N; i++) { // Traverse column for (int j = 0; j < N; j++) { System.out.print(M[i][j] + " "); } System.out.println(); } } // Driver Code public static void main(String[] args) { // Given size of matrix N int N = 3; // Function Call createMatrix(N); } } // This code is contributed by Ritik Bansal
Python3
# Python3 program for the above approach # Function to construct a N * N # matrix based on the given condition def createMatrix(N): # Matrix with given size M = [[0] * N for i in range(N)] # Counter to insert elements # from 1 to N * N count = 1 for i in range(N): # Check if it is first row # or odd row of the matrix if (i % 2 == 0): for j in range(N): # Insert elements from # left to right M[i][j] = count count += 1 # Condition if it is second # row or even row else: # Insert elements from # right to left for j in range(N - 1, -1, -1): M[i][j] = count count += 1 # Print the matrix for i in range(N): # Traverse column for j in range(N): print(M[i][j], end = " ") print() # Driver Code if __name__ == '__main__': # Given size of matrix N N = 3 # Function call createMatrix(N) # This code is contributed by mohit kumar 29
C#
// C# program for // the above approach using System; class GFG{ // Function to construct a N * N // matrix based on the given condition static void createMatrix(int N) { // Matrix with given sizM int[,] M = new int[N, N]; // Counter to insert elements // from 1 to N * N int count = 1; for (int i = 0; i < N; i++) { // Check if it is first row // or odd row of the matrix if (i % 2 == 0) { for (int j = 0; j < N; j++) { M[i, j] = count; count++; } } else { // Insert elements from // right to left for(int j = N - 1; j >= 0; j--) { M[i, j] = count; count += 1; } } } // Print the matrix for (int i = 0; i < N; i++) { // Traverse column for (int j = 0; j < N; j++) { Console.Write(M[i, j] + " "); } Console.WriteLine(); } } // Driver Code public static void Main() { // Given size of matrix N int N = 3; // Function Call createMatrix(N); } } // This code is contributed by Chitranayal
Javascript
<script> // javascript program for the above approach // Function to construct a N * N // matrix based on the given condition function createMatrix(N) { // Matrix with given sizM var M = Array(N).fill(0).map(x => Array(N).fill(0)); // Counter to insert elements // from 1 to N * N var count = 1; for (i = 0; i < N; i++) { // Check if it is first row // or odd row of the matrix if (i % 2 == 0) { for (j = 0; j < N; j++) { M[i][j] = count; count++; } } else { // Insert elements from // right to left for(j = N - 1; j >= 0; j--){ M[i][j] = count; count += 1 ; } } } // Print the matrix for (i = 0; i < N; i++) { // Traverse column for (j = 0; j < N; j++) { document.write(M[i][j] + " "); } document.write("<br>"); } } // Driver Code var N = 3; // Function Call createMatrix(N); // This code is contributed by 29AjayKumar </script>
1 2 3 6 5 4 7 8 9
Complejidad temporal: O(N*N)
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por mishrapriyanshu557 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA