📌  相关文章
📜  使用交换最小化两个数组中最大数的乘积

📅  最后修改于: 2022-05-13 01:56:08.377000             🧑  作者: Mango

使用交换最小化两个数组中最大数的乘积

给定 2 个大小为N的数组arr1[]arr2[] ,任务是找到两个数组的最大值最小乘积 通过执行任意次数的交换操作。在一次交换操作中,选择数组的任何索引并交换arr1[index]arr2[index]

例子:

方法:可以使用指针排序来解决任务。请按照以下步骤解决问题:

  1. 从运行一个循环 索引 = 0到索引 = n -1
  2. 如果arr1[index] < arr2[index]交换这些值。
  3. 最后,对两个数组进行排序,然后 得到arr1[n-1]*arr2[n-1]作为所需答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
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



输出
4

时间复杂度:O(NlogN)
辅助空间:O(N)

高效方法:使用交换最小化两个数组中最大数的乘积|设置 2