Dado un entero positivo N , la tarea es construir una array de tamaño N * N tal que todos los elementos de la array sean distintos del rango [1, N 2 ] y la suma de los elementos en ambas diagonales de cada 2 * 2 subarrays incluso.
Ejemplos:
Entrada: N = 3
Salida:
1 2 3
4 5 6
7 8 9
Explicación:
Los elementos diagonales de cada array 2 * 2 en la array de salida son { {1, 5}, {2, 4}, {2, 6}, {3, 5}, {4, 8}, {5, 7}, {5, 9}, {6, 8} }. Se puede observar que la suma de todas las diagonales es par.Entrada: N = 4
Salida:
1 2 3 4
6 5 8 7
9 10 11 12
14 13 16 15
Enfoque: siga los pasos a continuación para resolver el problema:
- Inicialice una array, digamos mat[][] , para almacenar los elementos de la array de modo que todos los elementos de la array sean distintos del rango [1, N 2 ] y la suma de los elementos de la array en ambas diagonales de cada 2 * 2 subarrays sea incluso.
- Inicialice una variable, digamos impar = 1 , para almacenar números impares.
- Inicializa una variable, digamos even = 2 , para almacenar números pares.
- Rellene todos los elementos de la array, mat[i][j] , comprobando las siguientes condiciones:
- Si (i + j) % 2 = 0 , entonces establezca mat[i][j] = impar y actualice impar += 2 .
- De lo contrario, establezca mat[i][j] = even y actualice even += 2 .
- Finalmente, imprima la array mat[][] .
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 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
<script> // Javascript program of the above approach // Function to construct a matrix such that // the sum elements in both diagonals of // every 2 * 2 matrices is even function generateMatrix(N) { // Stores odd numbers let odd = 1; // Stores even numbers let even = 2; // Store matrix elements such that // sum of elements in both diagonals // of every 2 * 2 submatrices is even let mat = new Array(N + 1); // Loop to create 2D array using 1D array for (var i = 0; i < mat.length; i++) { mat[i] = new Array(2); } // Fill all the values of // matrix elements for(let i = 1; i <= N; i++) { for(let 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(let i = 1; i <= N; i++) { for(let j = 1; j <= N; j++) { document.write(mat[i][j] + " "); } document.write("<br/>"); } } // Driver Code let N = 4; generateMatrix(N); </script>
1 2 3 4 6 5 8 7 9 10 11 12 14 13 16 15
Tiempo Complejidad: O(N 2 )
Espacio Auxiliar: O(N 2 )
Publicación traducida automáticamente
Artículo escrito por dharanendralv23 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA