Minimize product of maximum numbers in two Array using swaps | Set 2
Last Updated :
28 Mar, 2022
Given two arrays arr1[] and arr2[] of N size each. Select any index i and swap the element arr1[i] and arr2[i]. By applying this operation any number of times(possibly none) find the minimum possible product of maximum elements from array arr1[] and arr2[].
Examples:
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.
Approach: The solution is based on simple observation. Just try to bring the smaller elements in one array and the bigger on another by using swap. This will minimize the maximum element in one array and will result in minimum product. Follow the steps mentioned below:
- Here first select any array, either arr1[] or arr2[] (say selected arr1[]).
- Swap all the elements which are at same index but satisfy the condition arr1[i] < arr2[i] (means bringing the larger elements in arr1[]).
- After swapping all the pairs in between array arr1[] and arr2[], just find the maximum in both the arrays.
- Now just output the product of maximum of both the arrays.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int answer( int arr1[], int arr2[], int N)
{
for ( int i = 0; i < N; i++) {
if (arr1[i] < arr2[i]) {
swap(arr1[i], arr2[i]);
}
}
int max1 = INT_MIN, max2 = INT_MIN;
for ( int i = 0; i < N; i++) {
if (arr1[i] > max1) {
max1 = arr1[i];
}
}
for ( int i = 0; i < N; i++) {
if (arr2[i] > max2) {
max2 = arr2[i];
}
}
return max1 * max2;
}
int main()
{
int N = 6;
int arr1[6] = { 1, 2, 6, 5, 1, 2 };
int arr2[6] = { 3, 4, 3, 2, 2, 5 };
cout << answer(arr1, arr2, N);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static int answer( int [] arr1, int [] arr2, int N)
{
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;
for ( int i = 0 ; i < N; i++) {
if (arr1[i] > max1) {
max1 = arr1[i];
}
}
for ( int i = 0 ; i < N; i++) {
if (arr2[i] > max2) {
max2 = arr2[i];
}
}
return max1 * max2;
}
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 };
System.out.print(answer(arr1, arr2, N));
}
}
|
Python
import sys
def answer(arr1, arr2, N):
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
for i in range ( 0 , N):
if (arr1[i] > max1):
max1 = arr1[i]
for i in range ( 0 , N):
if (arr2[i] > max2):
max2 = arr2[i]
return max1 * max2
N = 6
arr1 = [ 1 , 2 , 6 , 5 , 1 , 2 ]
arr2 = [ 3 , 4 , 3 , 2 , 2 , 5 ]
print (answer(arr1, arr2, N))
|
C#
using System;
class GFG {
public static int answer( int [] arr1, int [] arr2, int N)
{
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;
for ( int i = 0; i < N; i++) {
if (arr1[i] > max1) {
max1 = arr1[i];
}
}
for ( int i = 0; i < N; i++) {
if (arr2[i] > max2) {
max2 = arr2[i];
}
}
return max1 * max2;
}
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 };
Console.Write(answer(arr1, arr2, N));
}
}
|
Javascript
<script>
function answer(arr1, arr2, N)
{
for (let i = 0; i < N; i++) {
if (arr1[i] < arr2[i]) {
let temp = arr1[i];
arr1[i] = arr2[i]
arr2[i] = temp;
}
}
let max1 = Number.MIN_VALUE;
let max2 = Number.MIN_VALUE;
for (let i = 0; i < N; i++) {
if (arr1[i] > max1) {
max1 = arr1[i];
}
}
for (let i = 0; i < N; i++) {
if (arr2[i] > max2) {
max2 = arr2[i];
}
}
return max1 * max2;
}
let N = 6;
let arr1 = [1, 2, 6, 5, 1, 2];
let arr2 = [3, 4, 3, 2, 2, 5];
document.write(answer(arr1, arr2, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...