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++ code to implement the approach #include <bits/stdc++.h> 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 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 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# 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. |
<script> // JavaScript code for the above approach
function 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 (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;
// Find the biggest element in arr1[]
for (let i = 0; i < N; i++) {
if (arr1[i] > max1) {
max1 = arr1[i];
}
}
// Find the biggest element in arr2[]
for (let i = 0; i < N; i++) {
if (arr2[i] > max2) {
max2 = arr2[i];
}
}
return max1 * max2;
}
// Driver code
let N = 6;
let arr1 = [1, 2, 6, 5, 1, 2];
let arr2 = [3, 4, 3, 2, 2, 5];
// Print the answer by calling function
document.write(answer(arr1, arr2, N));
// This code is contributed by Potta Lokesh
</script>
|
18
Time Complexity: O(N)
Auxiliary Space: O(1)