Dadas 2 arrays arr1[] y arr2[] de tamaño N , la tarea es encontrar el producto mínimo de los máximos de ambas arrays realizando una operación de intercambio cualquier cantidad de veces. En una operación de intercambio, se selecciona cualquier índice de la array y se intercambian arr1[índice] y arr2[índice] .
Ejemplos:
Entrada: N = 2, arr1[2] = [1, 3], arr2[2] = [4, 1]
Salida: 4
Explicación: Tomando index = 1, intercambiando los valores de las arrays, arr1 se convierte en arr1[2] = [1. 1] y arr[2] = [4. 3]. Máximo de arr1 = 1 y máximo de arr2 = 4. Max(arr1)*Max(arr2) = 1*4 = 4.Entrada: N = 6, arr1[6] = [1, 2, 6, 5, 1, 1], arr2[6] = [3, 4, 3, 2, 2, 4]
Salida: 18
Enfoque: La tarea se puede resolver usando punteros y ordenando . Siga los pasos a continuación para resolver el problema:
- Ejecutar un bucle fromindex = 0 to index = n -1
- Si arr1[índice] < arr2[índice] intercambie estos valores.
- Al final, ordene las dos arrays y obtenga arr1[n-1]*arr2[n-1] como la respuesta 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 find the minimum product void minimumArrayProduct(int arr1[], int arr2[], int n) { // Iterate over the array and perform the swap // operations for (int index = 0; index < n; index++) { if (arr1[index] < arr2[index]) { swap(arr1[index], arr2[index]); } } // Sort the given arrays sort(arr1, arr1 + n); sort(arr2, arr2 + n); int ans = arr1[n - 1] * arr2[n - 1]; cout << ans << "\n"; } // Driver Code int main() { int n = 2; int arr1[2] = { 1, 3 }; int arr2[2] = { 4, 1 }; minimumArrayProduct(arr1, arr2, n); }
Java
// Java program for the above approach import java.util.*; class GFG { // Function to find the minimum product public static void minimumArrayProduct(int[] arr1, int[] arr2, int n) { // Iterate over the array and perform the swap // operations for (int index = 0; index < n; index++) { if (arr1[index] < arr2[index]) { int temp = arr1[index]; arr1[index] = arr2[index]; arr2[index] = temp; } } // Sort the given arrays Arrays.sort(arr1); Arrays.sort(arr2); int ans = arr1[n - 1] * arr2[n - 1]; System.out.println(ans); } // Driver Code public static void main(String[] args) { int n = 2; int[] arr1 = new int[] { 1, 3 }; int[] arr2 = new int[] { 4, 1 }; minimumArrayProduct(arr1, arr2, n); } } // This code is contributed by Taranpreet
Python3
# Python code for the above approach # Function to find the minimum product def minimumArrayProduct(arr1, arr2, n): # Iterate over the array and perform the swap # operations for index in range(n): if (arr1[index] < arr2[index]): temp = arr1[index] arr1[index] = arr2[index] arr2[index] = temp # Sort the given arrays arr1.sort() arr2.sort() ans = arr1[n - 1] * arr2[n - 1] print(ans) # Driver Code n = 2 arr1 = [1, 3] arr2 = [4, 1] minimumArrayProduct(arr1, arr2, n) # This code is contributed by gfgking
C#
// C# program for the above approach using System; public class GFG { // Function to find the minimum product static void minimumArrayProduct(int[] arr1, int[] arr2, int n) { // Iterate over the array and perform the swap // operations for (int index = 0; index < n; index++) { if (arr1[index] < arr2[index]) { int temp = arr1[index]; arr1[index] = arr2[index]; arr2[index] = temp; } } // Sort the given arrays Array.Sort(arr1); Array.Sort(arr2); int ans = arr1[n - 1] * arr2[n - 1]; Console.Write(ans); } // Driver Code public static void Main(String[] args) { int n = 2; int[] arr1 = new int[] { 1, 3 }; int[] arr2 = new int[] { 4, 1 }; minimumArrayProduct(arr1, arr2, n); } } // This code is contributed by code_hunt.
Javascript
<script> // JavaScript code for the above approach // Function to find the minimum product function minimumArrayProduct(arr1, arr2, n) { // Iterate over the array and perform the swap // operations for (let index = 0; index < n; index++) { if (arr1[index] < arr2[index]) { let temp = arr1[index]; arr1[index] = arr2[index] arr2[index] = temp; } } // Sort the given arrays arr1.sort(); arr2.sort(); let ans = arr1[n - 1] * arr2[n - 1]; document.write(ans + "<br>") } // Driver Code let n = 2; let arr1 = [1, 3]; let arr2 = [4, 1]; minimumArrayProduct(arr1, arr2, n); // This code is contributed by Potta Lokesh </script>
4
Complejidad de tiempo : O(NlogN)
Espacio auxiliar : O(N)
Enfoque eficiente: Minimice el producto de números máximos en dos arreglos usando intercambios | conjunto 2