使用交换最小化两个数组中最大数的乘积 |设置 2
给定两个N大小的数组arr1[]和arr2[] 。选择任何索引i并交换元素arr1[i]和arr2[i] 。通过多次应用此操作(可能没有)从数组arr1[]和arr2[]中找到最大元素的最小可能乘积。
例子:
Input: N = 6
arr1[] = {1, 2, 6, 5, 1, 2}
arr2[] = {3, 4, 3, 2, 2, 5}
Output: 18
Explanation: Swap elements at index 1 and 5. So arr1[] and arr2[] become
{1, 4, 6, 5, 1, 5} and {3, 2, 3, 2, 2, 2} respectively.
The maximum elements are 6 and 3 giving product as 18.
Input: N = 2
arr1[] = {1, 3}
arr2[] = {3, 1}
Output: 3
Explanation: Swap elements at index 1. arr1[] and arr2[] will become
{1, 1} and {3, 3}. The product of maximum elements will be 3.
方法:解决方案基于简单的观察。只需尝试使用交换将较小的元素放在一个数组中,将较大的元素放在另一个数组中。这将最小化一个数组中的最大元素,并产生最小的乘积。请按照以下步骤操作:
- 这里首先选择任何数组,要么arr1[]要么arr2[] (比如选择arr1[] )。
- 交换所有具有相同索引但满足条件arr1[i] < arr2[i]的元素(意味着将较大的元素带入arr1[] )。
- 交换数组arr1[] 和 arr2[]之间的所有对后,只需找到两个数组中的最大值。
- 现在只需输出两个数组的最大值的乘积。
下面是上述方法的实现。
C++
// C++ code to implement the approach
#include
using namespace std;
int answer(int arr1[], int arr2[], int N)
{
// Here select the array arr1[]
// and then swap all the integers
// at same index which
// are bigger in array arr2[]
for (int i = 0; i < N; i++) {
if (arr1[i] < arr2[i]) {
swap(arr1[i], arr2[i]);
}
}
int max1 = INT_MIN, max2 = INT_MIN;
// Find the biggest element in arr1[]
for (int i = 0; i < N; i++) {
if (arr1[i] > max1) {
max1 = arr1[i];
}
}
// Find the biggest element in arr2[]
for (int i = 0; i < N; i++) {
if (arr2[i] > max2) {
max2 = arr2[i];
}
}
return max1 * max2;
}
// Driver code
int main()
{
int N = 6;
int arr1[6] = { 1, 2, 6, 5, 1, 2 };
int arr2[6] = { 3, 4, 3, 2, 2, 5 };
// Print the answer by calling function
cout << answer(arr1, arr2, N);
return 0;
}
Java
// JAVA code to implement the approach
import java.util.*;
class GFG {
public static int answer(int[] arr1, int[] arr2, int N)
{
// Here select the array arr1[]
// and then swap all the integers
// at same index which
// are bigger in array arr2[]
for (int i = 0; i < N; i++) {
if (arr1[i] < arr2[i]) {
int temp = arr1[i];
arr1[i] = arr2[i];
arr2[i] = temp;
}
}
int max1 = Integer.MIN_VALUE, max2
= Integer.MIN_VALUE;
// Find the biggest element in arr1[]
for (int i = 0; i < N; i++) {
if (arr1[i] > max1) {
max1 = arr1[i];
}
}
// Find the biggest element in arr2[]
for (int i = 0; i < N; i++) {
if (arr2[i] > max2) {
max2 = arr2[i];
}
}
return max1 * max2;
}
// Driver code
public static void main(String[] args)
{
int N = 6;
int[] arr1 = new int[] { 1, 2, 6, 5, 1, 2 };
int[] arr2 = new int[] { 3, 4, 3, 2, 2, 5 };
// Print the answer by calling function
System.out.print(answer(arr1, arr2, N));
}
}
// This code is contributed by Taranpreet
Python
# Python code to implement the approach
import sys
def answer(arr1, arr2, N):
# Here select the array arr1[]
# and then swap all the integers
# at same index which
# are bigger in array arr2[]
for i in range(0, N):
if (arr1[i] < arr2[i]):
arr1[i], arr2[i] = arr2[i], arr1[i]
max1 = -sys.maxsize
max2 = -sys.maxsize
# Find the biggest element in arr1[]
for i in range(0, N):
if (arr1[i] > max1):
max1 = arr1[i]
# Find the biggest element in arr2[]
for i in range(0, N):
if (arr2[i] > max2):
max2 = arr2[i]
return max1 * max2
# Driver code
N = 6
arr1 = [ 1, 2, 6, 5, 1, 2 ]
arr2 = [ 3, 4, 3, 2, 2, 5 ]
# Print the answer by calling function
print(answer(arr1, arr2, N))
# This code is contributed Samim Hossain Mondal.
C#
// C# program to implement the approach
using System;
class GFG {
public static int answer(int[] arr1, int[] arr2, int N)
{
// Here select the array arr1[]
// and then swap all the integers
// at same index which
// are bigger in array arr2[]
for (int i = 0; i < N; i++) {
if (arr1[i] < arr2[i]) {
int temp = arr1[i];
arr1[i] = arr2[i];
arr2[i] = temp;
}
}
int max1 = Int32.MinValue, max2
= Int32.MinValue;
// Find the biggest element in arr1[]
for (int i = 0; i < N; i++) {
if (arr1[i] > max1) {
max1 = arr1[i];
}
}
// Find the biggest element in arr2[]
for (int i = 0; i < N; i++) {
if (arr2[i] > max2) {
max2 = arr2[i];
}
}
return max1 * max2;
}
// Driver code
public static void Main()
{
int N = 6;
int[] arr1 = new int[] { 1, 2, 6, 5, 1, 2 };
int[] arr2 = new int[] { 3, 4, 3, 2, 2, 5 };
// Print the answer by calling function
Console.Write(answer(arr1, arr2, N));
}
}
// This code is contributed by sanjoy_62.
Javascript
18
时间复杂度: O(N)
辅助空间: O(1)