Minimize product of maximum numbers in two Array using swaps
Last Updated :
14 Feb, 2022
Given 2 arrays arr1[] and arr2[] of size N the task is to find the minimum product of maximums of both arrays by performing a swapping operation any number of times. In one swap operation, any index of the array is selected and arr1[index] and arr2[index] are swapped.
Examples:
Input: N = 2, arr1[2] = [1, 3], arr2[2] = [4, 1]
Output: 4
Explanation: Taking index = 1, swapping the arrays values, arr1 becomes, arr1[2] = [1. 1] and arr[2] = [4. 3]. Maximum of arr1 = 1 and maximum of arr2 = 4. Max(arr1)*Max(arr2) = 1*4 = 4.
Input: N = 6, arr1[6] = [1, 2, 6, 5, 1, 1], arr2[6] = [3, 4, 3, 2, 2, 4]
Output: 18
Approach: The task can be solved using pointers and sorting. Follow the below steps to solve the problem:
- Run a loop from index = 0 to index = n -1
- If arr1[index] < arr2[index] swap these values .
- In the end, sort the two arrays, and get arr1[n-1]*arr2[n-1] as the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void minimumArrayProduct( int arr1[], int arr2[], int n)
{
for ( int index = 0; index < n; index++) {
if (arr1[index] < arr2[index]) {
swap(arr1[index], arr2[index]);
}
}
sort(arr1, arr1 + n);
sort(arr2, arr2 + n);
int ans = arr1[n - 1] * arr2[n - 1];
cout << ans << "\n" ;
}
int main()
{
int n = 2;
int arr1[2] = { 1, 3 };
int arr2[2] = { 4, 1 };
minimumArrayProduct(arr1, arr2, n);
}
|
Java
import java.util.*;
class GFG {
public static void
minimumArrayProduct( int [] arr1, int [] arr2, int n)
{
for ( int index = 0 ; index < n; index++) {
if (arr1[index] < arr2[index]) {
int temp = arr1[index];
arr1[index] = arr2[index];
arr2[index] = temp;
}
}
Arrays.sort(arr1);
Arrays.sort(arr2);
int ans = arr1[n - 1 ] * arr2[n - 1 ];
System.out.println(ans);
}
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);
}
}
|
Python3
def minimumArrayProduct(arr1, arr2, n):
for index in range (n):
if (arr1[index] < arr2[index]):
temp = arr1[index]
arr1[index] = arr2[index]
arr2[index] = temp
arr1.sort()
arr2.sort()
ans = arr1[n - 1 ] * arr2[n - 1 ]
print (ans)
n = 2
arr1 = [ 1 , 3 ]
arr2 = [ 4 , 1 ]
minimumArrayProduct(arr1, arr2, n)
|
C#
using System;
public class GFG
{
static void
minimumArrayProduct( int [] arr1, int [] arr2, int n)
{
for ( int index = 0; index < n; index++) {
if (arr1[index] < arr2[index]) {
int temp = arr1[index];
arr1[index] = arr2[index];
arr2[index] = temp;
}
}
Array.Sort(arr1);
Array.Sort(arr2);
int ans = arr1[n - 1] * arr2[n - 1];
Console.Write(ans);
}
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);
}
}
|
Javascript
<script>
function minimumArrayProduct(arr1, arr2, n)
{
for (let index = 0; index < n; index++) {
if (arr1[index] < arr2[index]) {
let temp = arr1[index];
arr1[index] = arr2[index]
arr2[index] = temp;
}
}
arr1.sort();
arr2.sort();
let ans = arr1[n - 1] * arr2[n - 1];
document.write(ans + "<br>" )
}
let n = 2;
let arr1 = [1, 3];
let arr2 = [4, 1];
minimumArrayProduct(arr1, arr2, n);
</script>
|
Time Complexity: O(NlogN)
Auxiliary Space: O(N)
Efficient Approach: Minimize product of maximum numbers in two Array using swaps | Set 2
Share your thoughts in the comments
Please Login to comment...