Dadas dos arrays A[] y B[] de N y M enteros respectivamente. También se proporciona una array binaria NXM donde 1 indica que había un número entero positivo en la array original y 0 indica que la posición se llena con 0 en la array original. La tarea es volver a formar la array original de modo que A[i] indique el elemento más grande en la i -ésima fila y B[j] indique el elemento más grande en la j -ésima columna.
Ejemplos:
Input: A[] = {2, 1, 3}, B[] = {2, 3, 0, 0, 2, 0, 1}, matrix[] = {{1, 0, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 1}, {1, 1, 0, 0, 0, 0, 0}} Output: 2 0 0 0 2 0 0 0 0 0 0 0 0 1 2 3 0 0 0 0 0 Input: A[] = {2, 4}, B[] = {4, 2}, matrix[] = {{1, 1}, {1, 1}} Output: 2 2 4 2
Enfoque: iterar para cada índice (i, j) en la array, y si mat[i][j] == 1 , luego llene la posición con min(A[i], B[j]) . Esto se debe a que el elemento actual es parte de la i -ésima fila y la j -ésima columna y si se eligió el máximo (A[i], B[j]) , entonces una de las condiciones no podría cumplirse, es decir, el elemento elegido podría exceder ya sea el elemento máximo requerido en la fila actual o la columna actual.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; #define N 3 #define M 7 // Function that prints the original matrix void printOriginalMatrix(int a[], int b[], int mat[N][M]) { // Iterate in the row for (int i = 0; i < N; i++) { // Iterate in the column for (int j = 0; j < M; j++) { // If previously existed an element if (mat[i][j] == 1) cout << min(a[i], b[j]) << " "; else cout << 0 << " "; } cout << endl; } } // Driver code int main() { int a[] = { 2, 1, 3 }; int b[] = { 2, 3, 0, 0, 2, 0, 1 }; int mat[N][M] = { { 1, 0, 0, 0, 1, 0, 0 }, { 0, 0, 0, 0, 0, 0, 1 }, { 1, 1, 0, 0, 0, 0, 0 } }; printOriginalMatrix(a, b, mat); return 0; }
Java
// Java implementation of the approach class GFG { static int N = 3; static int M = 7; // Function that prints the original matrix static void printOriginalMatrix(int a[], int b[], int[][] mat) { // Iterate in the row for (int i = 0; i < N; i++) { // Iterate in the column for (int j = 0; j < M; j++) { // If previously existed an element if (mat[i][j] == 1) System.out.print(Math.min(a[i], b[j]) + " "); else System.out.print("0" + " "); } System.out.println(); } } // Driver code public static void main(String[] args) { int a[] = { 2, 1, 3 }; int b[] = { 2, 3, 0, 0, 2, 0, 1 }; int[][] mat = {{ 1, 0, 0, 0, 1, 0, 0 }, { 0, 0, 0, 0, 0, 0, 1 }, { 1, 1, 0, 0, 0, 0, 0 }}; printOriginalMatrix(a, b, mat); } } // This code is contributed by Code_Mech
Python3
# Python3 implementation of the approach N = 3 M = 7 # Function that prints the original matrix def printOriginalMatrix(a, b, mat) : # Iterate in the row for i in range(N) : # Iterate in the column for j in range(M) : # If previously existed an element if (mat[i][j] == 1) : print(min(a[i], b[j]), end = " "); else : print(0, end = " "); print() # Driver code if __name__ == "__main__" : a = [ 2, 1, 3 ] b = [ 2, 3, 0, 0, 2, 0, 1 ] mat = [[ 1, 0, 0, 0, 1, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 1 ], [ 1, 1, 0, 0, 0, 0, 0 ]]; printOriginalMatrix(a, b, mat); # This code is contributed by Ryuga
C#
// C# implementation of the approach using System; class GFG { static int N = 3; static int M = 7; // Function that prints the original matrix static void printOriginalMatrix(int[] a, int[] b, int[,] mat) { // Iterate in the row for (int i = 0; i < N; i++) { // Iterate in the column for (int j = 0; j < M; j++) { // If previously existed an element if (mat[i,j] == 1) Console.Write(Math.Min(a[i], b[j]) + " "); else Console.Write("0" + " "); } Console.WriteLine(); } } // Driver code public static void Main() { int[] a = { 2, 1, 3 }; int[] b = { 2, 3, 0, 0, 2, 0, 1 }; int[,] mat = {{ 1, 0, 0, 0, 1, 0, 0 }, { 0, 0, 0, 0, 0, 0, 1 }, { 1, 1, 0, 0, 0, 0, 0 }}; printOriginalMatrix(a, b, mat); } } // This code is contributed by Code_Mech
PHP
<?php // PHP implementation of the approach $N = 3; $M = 7; // Function that prints the original matrix function printOriginalMatrix($a, $b, $mat) { // Iterate in the row for ($i = 0; $i < $GLOBALS['N']; $i++) { // Iterate in the column for ($j = 0; $j < $GLOBALS['M']; $j++) { // If previously existed an element if ($mat[$i][$j] == 1) echo min($a[$i], $b[$j])." "; else echo "0"." "; } echo "\r\n"; } } // Driver code $a = array( 2, 1, 3 ); $b = array(2, 3, 0, 0, 2, 0, 1 ); $mat = array( array( 1, 0, 0, 0, 1, 0, 0 ), array( 0, 0, 0, 0, 0, 0, 1 ), array( 1, 1, 0, 0, 0, 0, 0 )); printOriginalMatrix($a, $b, $mat); // This code is contributed by Shashank_Sharma ?>
Javascript
<script> // Javascript implementation of the approach let N = 3; let M = 7; // Function that prints the original matrix function printOriginalMatrix(a,b,mat) { // Iterate in the row for (let i = 0; i < N; i++) { // Iterate in the column for (let j = 0; j < M; j++) { // If previously existed an element if (mat[i][j] == 1) document.write(Math.min(a[i], b[j]) + " "); else document.write("0" + " "); } document.write("<br>"); } } // Driver code let a = [ 2, 1, 3 ]; let b = [ 2, 3, 0, 0, 2, 0, 1 ]; let mat = [[ 1, 0, 0, 0, 1, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 1 ], [ 1, 1, 0, 0, 0, 0, 0 ]]; printOriginalMatrix(a, b, mat); // This code is contributed Bobby </script>
2 0 0 0 2 0 0 0 0 0 0 0 0 1 2 3 0 0 0 0 0
Complejidad de Tiempo: O(N * M)
Espacio Auxiliar: O(1)