Open In App

# Product of maximum in first array and minimum in second

Given two arrays, the task is to calculate the product of max element of first array and min element of second array

Examples :

```Input : arr1[] = {5, 7, 9, 3, 6, 2},
arr2[] = {1, 2, 6, -1, 0, 9}
Output : max element in first array
is 9 and min element in second array
is -1. The product of these two is -9.

Input : arr1[] = {1, 4, 2, 3, 10, 2},
arr2[] = {4, 2, 6, 5, 2, 9}
Output : max element in first array
is 10 and min element in second array
is 2. The product of these two is 20.```

Method 1:

Naive approach We first sort both arrays. Then we easily find max in first array and min in second array. Finally, we return product of min and max.

Below is the implementation of the above approach:

## C++

 `// C++ program to calculate the``// product of max element of``// first array and min element``// of second array``#include ``using` `namespace` `std;` `// Function to calculate``// the product``int` `minMaxProduct(``int` `arr1[],``                  ``int` `arr2[],``                  ``int` `n1,``                  ``int` `n2)``{``    ``// Sort the arrays to find``    ``// the maximum and minimum``    ``// elements in given arrays``    ``sort(arr1, arr1 + n1);``    ``sort(arr2, arr2 + n2);` `    ``// Return product of``    ``// maximum and minimum.``    ``return` `arr1[n1 - 1] * arr2[0];``}` `// Driven code``int` `main()``{``    ``int` `arr1[] = { 10, 2, 3, 6, 4, 1 };``    ``int` `arr2[] = { 5, 1, 4, 2, 6, 9 };``    ``int` `n1 = ``sizeof``(arr1) / ``sizeof``(arr1[0]);``    ``int` `n2 = ``sizeof``(arr1) / ``sizeof``(arr1[0]);``    ``cout << minMaxProduct(arr1, arr2, n1, n2);``    ``return` `0;``}`

## Java

 `// Java program to find the``// to calculate the product``// of max element of first``// array and min element of``// second array``import` `java.util.*;``import` `java.lang.*;` `class` `GfG``{` `    ``// Function to calculate``    ``// the product``    ``public` `static` `int` `minMaxProduct(``int` `arr1[],``                                    ``int` `arr2[],``                                    ``int` `n1,``                                    ``int` `n2)``    ``{` `        ``// Sort the arrays to find the``        ``// maximum and minimum elements``        ``// in given arrays``        ``Arrays.sort(arr1);``        ``Arrays.sort(arr2);` `        ``// Return product of maximum``        ``// and minimum.``        ``return` `arr1[n1 - ``1``] * arr2[``0``];``    ``}``    ` `    ``// Driver Code``    ``public` `static` `void` `main(String argc[])``    ``{``        ``int` `[] arr1= ``new` `int` `[]{ ``10``, ``2``, ``3``,``                                  ``6``, ``4``, ``1` `};``        ``int` `[] arr2 = ``new` `int` `[]{ ``5``, ``1``, ``4``,``                                  ``2``, ``6``, ``9` `};``        ``int` `n1 = ``6``;``        ``int` `n2 = ``6``;``        ``System.out.println(minMaxProduct(arr1,``                                         ``arr2,``                                         ``n1, n2));``    ``}``}` `/*This code is contributed by Sagar Shukla.*/`

## Python

 `# A Python program to find the to``# calculate the product of max``# element of first array and min``# element of second array` `# Function to calculate the product``def` `minmaxProduct(arr1, arr2, n1, n2):` `    ``# Sort the arrays to find the``    ``# maximum and minimum elements``    ``# in given arrays``    ``arr1.sort()``    ``arr2.sort()` `    ``# Return product of maximum``    ``# and minimum.``    ``return` `arr1[n1 ``-` `1``] ``*` `arr2[``0``]` `# Driver Program``arr1 ``=` `[``10``, ``2``, ``3``, ``6``, ``4``, ``1``]``arr2 ``=` `[``5``, ``1``, ``4``, ``2``, ``6``, ``9``]``n1 ``=` `len``(arr1)``n2 ``=` `len``(arr2)``print``(minmaxProduct(arr1, arr2, n1, n2))` `# This code is contributed by Shrikant13.`

## C#

 `// C# program to find the to``// calculate the product of``// max element of first array``// and min element of second array``using` `System;` `class` `GfG``{` `    ``// Function to calculate the product``    ``public` `static` `int` `minMaxProduct(``int` `[]arr1,``                                    ``int` `[]arr2,``                                    ``int` `n1,``                                    ``int` `n2)``    ``{` `        ``// Sort the arrays to find the``        ``// maximum and minimum elements``        ``// in given arrays``        ``Array.Sort(arr1);``        ``Array.Sort(arr2);` `        ``// Return product of maximum``        ``// and minimum.``        ``return` `arr1[n1 - 1] * arr2[0];``    ``}``    ` `    ``// Driver Code``    ``public` `static` `void` `Main()``    ``{``        ``int` `[] arr1= ``new` `int` `[]{ 10, 2, 3,``                                 ``6, 4, 1 };``        ``int` `[] arr2 = ``new` `int` `[]{ 5, 1, 4,``                                  ``2, 6, 9 };``        ``int` `n1 = 6;``        ``int` `n2 = 6;``        ``Console.WriteLine(minMaxProduct(arr1, arr2,``                                        ``n1, n2));``    ``}``}` `/*This code is contributed by vt_m.*/`

## PHP

 ``

## Javascript

 ``

Output

`10`

Time Complexity: O(n log n)
Auxiliary Space: O(1)

Efficient approach: In this approach, we simply traverse the whole arrays and find max in first array and min in second array and can easily get product of min and max.

Below is the implementation of the above approach:

## C++

 `// C++ program to find the to``// calculate the product of``// max element of first array``// and min element of second array``#include ``using` `namespace` `std;` `// Function to calculate the product``int` `minMaxProduct(``int` `arr1[], ``int` `arr2[],``                  ``int` `n1, ``int` `n2)``{``    ``// Initialize max of first array``    ``int` `max = arr1[0];` `    ``// initialize min of second array``    ``int` `min = arr2[0];` `    ``int` `i;``    ``for` `(i = 1; i < n1 && i < n2; ++i)``    ``{` `        ``// To find the maximum``        ``// element in first array``        ``if` `(arr1[i] > max)``            ``max = arr1[i];` `        ``// To find the minimum``        ``// element in second array``        ``if` `(arr2[i] < min)``            ``min = arr2[i];``    ``}` `    ``// Process remaining elements``    ``while` `(i < n1)``    ``{``        ``if` `(arr1[i] > max)``        ``max = arr1[i];``        ``i++;``    ``}``    ``while` `(i < n2)``    ``{``        ``if` `(arr2[i] < min)``        ``min = arr2[i];``        ``i++;``    ``}` `    ``return` `max * min;``}` `// Driven code``int` `main()``{``    ``int` `arr1[] = { 10, 2, 3, 6, 4, 1 };``    ``int` `arr2[] = { 5, 1, 4, 2, 6, 9 };``    ``int` `n1 = ``sizeof``(arr1) / ``sizeof``(arr1[0]);``    ``int` `n2 = ``sizeof``(arr1) / ``sizeof``(arr1[0]);``    ``cout << minMaxProduct(arr1, arr2, n1, n2)``         ``<< endl;``    ``return` `0;``}`

## Java

 `// Java program to calculate the``// product of max element of first``// array and min element of second array``import` `java.util.*;``import` `java.lang.*;` `class` `GfG``{` `    ``// Function to calculate the product``    ``public` `static` `int` `minMaxProduct(``int` `arr1[],``                                    ``int` `arr2[],``                                    ``int` `n1,``                                    ``int` `n2)``       ``{` `        ``// Initialize max of``        ``// first array``        ``int` `max = arr1[``0``];` `        ``// initialize min of``        ``// second array``        ``int` `min = arr2[``0``];` `        ``int` `i;``        ``for` `(i = ``1``; i < n1 && i < n2; ++i)``        ``{` `        ``// To find the maximum``        ``// element in first array``        ``if` `(arr1[i] > max)``            ``max = arr1[i];` `        ``// To find the minimum element``        ``// in second array``        ``if` `(arr2[i] < min)``            ``min = arr2[i];``        ``}` `        ``// Process remaining elements``        ``while` `(i < n1)``        ``{``            ``if` `(arr1[i] > max)``            ``max = arr1[i];``            ``i++;``        ``}``        ``while` `(i < n2)``        ``{``            ``if` `(arr2[i] < min)``            ``min = arr2[i];``            ``i++;``        ``}` `        ``return` `max * min;``    ``}``    ` `    ``// Driver Code``    ``public` `static` `void` `main(String argc[])``    ``{``        ``int` `[] arr1= ``new` `int` `[]{ ``10``, ``2``, ``3``,``                                 ``6``, ``4``, ``1` `};``        ``int` `[] arr2 = ``new` `int` `[]{ ``5``, ``1``, ``4``,``                                  ``2``, ``6``, ``9` `};``        ``int` `n1 = ``6``;``        ``int` `n2 = ``6``;``        ``System.out.println(minMaxProduct(arr1, arr2,``                                          ``n1, n2));``    ``}``}` `// This code is contributed by Sagar Shukla`

## Python3

 `# Python3 program to find the to``# calculate the product of``# max element of first array``# and min element of second array` `# Function to calculate the product``def` `minMaxProduct(arr1, arr2,``                  ``n1, n2) :` `    ``# Initialize max of first array``    ``max` `=` `arr1[``0``]` `    ``# initialize min of second array``    ``min` `=` `arr2[``0``]``    ` `    ``i ``=` `1``    ``while` `(i < n1 ``and` `i < n2) :``    ` `        ``# To find the maximum``        ``# element in first array``        ``if` `(arr1[i] > ``max``) :``            ``max` `=` `arr1[i]` `        ``# To find the minimum``        ``# element in second array``        ``if` `(arr2[i] < ``min``) :``            ``min` `=` `arr2[i]``        ` `        ``i ``+``=` `1` `    ``# Process remaining elements``    ``while` `(i < n1) :``    ` `        ``if` `(arr1[i] > ``max``) :``            ``max` `=` `arr1[i]``            ``i ``+``=` `1``    ` `    ``while` `(i < n2):``    ` `        ``if` `(arr2[i] < ``min``) :``            ``min` `=` `arr2[i]``            ``i ``+``=` `1` `    ``return` `max` `*` `min` `# Driver code``arr1 ``=` `[``10``, ``2``, ``3``, ``6``, ``4``, ``1` `]``arr2 ``=` `[``5``, ``1``, ``4``, ``2``, ``6``, ``9` `]``n1 ``=` `len``(arr1)``n2 ``=` `len``(arr1)``print``(minMaxProduct(arr1, arr2, n1, n2))` `# This code is contributed by Smitha`

## C#

 `// C# program to find the to``// calculate the product of``// max element of first array``// and min element of second array``using` `System;` `class` `GfG``{` `    ``// Function to calculate``    ``// the product``    ``public` `static` `int` `minMaxProduct(``int` `[]arr1,``                                    ``int` `[]arr2,``                                    ``int` `n1,``                                    ``int` `n2)``    ``{` `        ``// Initialize max of``        ``// first array``        ``int` `max = arr1[0];` `        ``// initialize min of``        ``// second array``        ``int` `min = arr2[0];` `        ``int` `i;``        ``for` `(i = 1; i < n1 && i < n2; ++i)``        ``{` `            ``// To find the maximum element``            ``// in first array``            ``if` `(arr1[i] > max)``                ``max = arr1[i];``    ` `            ``// To find the minimum element``            ``// in second array``            ``if` `(arr2[i] < min)``                ``min = arr2[i];``        ``}` `        ``// Process remaining elements``        ``while` `(i < n1)``        ``{``            ``if` `(arr1[i] > max)``            ``max = arr1[i];``            ``i++;``        ``}``        ``while` `(i < n2)``        ``{``            ``if` `(arr2[i] < min)``            ``min = arr2[i];``            ``i++;``        ``}` `        ``return` `max * min;``    ``}``    ` `    ``// Driver Code``    ``public` `static` `void` `Main()``    ``{``        ``int` `[] arr1= ``new` `int` `[]{ 10, 2, 3,``                                 ``6, 4, 1 };``        ``int` `[] arr2 = ``new` `int` `[]{ 5, 1, 4,``                                  ``2, 6, 9 };``        ``int` `n1 = 6;``        ``int` `n2 = 6;``        ``Console.WriteLine(minMaxProduct(arr1, arr2,``                                        ``n1, n2));``    ``}``}` `// This code is contributed by vt_m`

## PHP

 ` ``\$max``)``            ``\$max` `= ``\$arr1``[``\$i``];` `        ``// To find the minimum element``        ``// in second array``        ``if` `(``\$arr2``[``\$i``] < ``\$min``)``            ``\$min` `= ``\$arr2``[``\$i``];``    ``}` `    ``// Process remaining elements``    ``while` `(``\$i` `< ``\$n1``)``    ``{``        ``if` `(``\$arr1``[``\$i``] > ``\$max``)``        ``\$max` `= ``\$arr1``[``\$i``];``        ``\$i``++;``    ``}``    ``while` `(``\$i` `< ``\$n2``)``    ``{``        ``if` `(``\$arr2``[``\$i``] < ``\$min``)``        ``\$min` `= ``\$arr2``[``\$i``];``        ``\$i``++;``    ``}` `    ``return` `\$max` `* ``\$min``;``}` `    ``// Driven code``    ``\$arr1` `= ``array``(10, 2, 3,``                  ``6, 4, 1);``    ``\$arr2` `= ``array``(5, 1, 4,``                  ``2, 6, 9);``    ``\$n1` `= ``count``(``\$arr1``);``    ``\$n2` `= ``count``(``\$arr2``);``    ``echo` `minMaxProduct(``\$arr1``, ``\$arr2``,``                       ``\$n1``, ``\$n2``);``    ` `// This code is contributed by anuj_67.``?>`

## Javascript

 ``

Output

`10`

Time Complexity : O(n)
Auxiliary Space: O(1)

#### Approach#2: Using for loop

One straightforward approach is to find the maximum element in the first array and the minimum element in the second array and multiply them to get the desired product.

#### Algorithm

1. Initialize a variable “max1” to arr1[0] and a variable “min2” to arr2[0].
2. Traverse the first array “arr1” from index 1 to n-1 and update “max1” if the current element is greater than the current value of “max1”.
3. Traverse the second array “arr2” from index 1 to n-1 and update “min2” if the current element is less than the current value of “min2”.
4. Return the product of “max1” and “min2”.

## C++

 `#include ``#include ``using` `namespace` `std;` `int` `max_min_product(vector<``int``> arr1, vector<``int``> arr2)``{``    ``// Initialize max1 to the first element of arr1``    ``int` `max1 = arr1[0];``  ` `    ``// Initialize min2 to the first element of arr2``    ``int` `min2 = arr2[0];``  ` `    ``// Loop through arr1 starting from the second element``    ``for` `(``int` `i = 1; i < arr1.size(); i++)``    ``{``      ` `        ``// If the current element is greater than max1,``        ``// update max1``        ``if` `(arr1[i] > max1) {``            ``max1 = arr1[i];``        ``}``    ``}``    ``// Loop through arr2 starting from the second element``    ``for` `(``int` `i = 1; i < arr2.size(); i++) {``        ``// If the current element is less than min2, update``        ``// min2``        ``if` `(arr2[i] < min2) {``            ``min2 = arr2[i];``        ``}``    ``}``  ` `    ``// Return the product of max1 and min2``    ``return` `max1 * min2;``}` `int` `main()``{``    ``vector<``int``> arr1 = { 10, 2, 3, 6, 4, 1 };``    ``vector<``int``> arr2 = { 5, 1, 4, 2, 6, 9 };``    ``cout << max_min_product(arr1, arr2) << endl;``    ``return` `0;``}`

## Python3

 `def` `max_min_product(arr1, arr2):``    ``max1 ``=` `arr1[``0``]``    ``min2 ``=` `arr2[``0``]``    ``for` `i ``in` `range``(``1``, ``len``(arr1)):``        ``if` `arr1[i] > max1:``            ``max1 ``=` `arr1[i]``    ``for` `i ``in` `range``(``1``, ``len``(arr2)):``        ``if` `arr2[i] < min2:``            ``min2 ``=` `arr2[i]``    ``return` `max1 ``*` `min2``arr1 ``=` `[``10``, ``2``, ``3``, ``6``, ``4``, ``1``]``arr2 ``=` `[``5``, ``1``, ``4``, ``2``, ``6``, ``9``]``print``(max_min_product(arr1, arr2))`

## C#

 `using` `System;``using` `System.Collections.Generic;``using` `System.Linq;` `class` `Program {``  ``static` `int` `MaxMinProduct(List<``int``> arr1, List<``int``> arr2)``  ``{``    ` `    ``// Initialize max1 to the first element of arr1``    ``int` `max1 = arr1[0];` `    ``// Initialize min2 to the first element of arr2``    ``int` `min2 = arr2[0];` `    ``// Loop through arr1 starting from the second``    ``// element``    ``for` `(``int` `i = 1; i < arr1.Count(); i++) {``      ``// If the current element is greater than max1,``      ``// update max1``      ``if` `(arr1[i] > max1) {``        ``max1 = arr1[i];``      ``}``    ``}` `    ``// Loop through arr2 starting from the second``    ``// element``    ``for` `(``int` `i = 1; i < arr2.Count(); i++) {``      ``// If the current element is less than min2,``      ``// update min2``      ``if` `(arr2[i] < min2) {``        ``min2 = arr2[i];``      ``}``    ``}` `    ``// Return the product of max1 and min2``    ``return` `max1 * min2;``  ``}` `  ``static` `void` `Main(``string``[] args)``  ``{``    ``List<``int``> arr1 = ``new` `List<``int``>{ 10, 2, 3, 6, 4, 1 };``    ``List<``int``> arr2 = ``new` `List<``int``>{ 5, 1, 4, 2, 6, 9 };``    ``Console.WriteLine(MaxMinProduct(arr1, arr2));``  ``}``}`

## Javascript

 `function` `max_min_product(arr1, arr2) {``    ``let max1 = arr1[0];``    ``let min2 = arr2[0];``    ``for` `(let i = 1; i < arr1.length; i++) {``        ``if` `(arr1[i] > max1) {``            ``max1 = arr1[i];``        ``}``    ``}``    ``for` `(let i = 1; i < arr2.length; i++) {``        ``if` `(arr2[i] < min2) {``            ``min2 = arr2[i];``        ``}``    ``}``    ``return` `max1 * min2;``}` `let arr1 = [10, 2, 3, 6, 4, 1];``let arr2 = [5, 1, 4, 2, 6, 9];``console.log(max_min_product(arr1, arr2));` `// Contributed by adityasha4x71`

## Java

 `import` `java.util.*;` `public` `class` `Main {``    ``public` `static` `int` `maxMinProduct(ArrayList arr1,``                                    ``ArrayList arr2)``    ``{``        ``// Initialize max1 to the first``        ``// element of arr1``        ``int` `max1 = arr1.get(``0``);` `        ``// Initialize min2 to the``        ``// first element of arr2``        ``int` `min2 = arr2.get(``0``);` `        ``// Loop through arr1 starting from``        ``// the second element``        ``for` `(``int` `i = ``1``; i < arr1.size(); i++) {` `            ``// If the current element is greater``            ``// than max1, update max1``            ``if` `(arr1.get(i) > max1) {``                ``max1 = arr1.get(i);``            ``}``        ``}` `        ``// Loop through arr2 starting from``        ``// the second element``        ``for` `(``int` `i = ``1``; i < arr2.size(); i++) {` `            ``// If the current element is``            ``// less than min2, update min2``            ``if` `(arr2.get(i) < min2) {``                ``min2 = arr2.get(i);``            ``}``        ``}` `        ``// Return the product of max1 and min2``        ``return` `max1 * min2;``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``ArrayList arr1 = ``new` `ArrayList<>(``            ``Arrays.asList(``10``, ``2``, ``3``, ``6``, ``4``, ``1``));``        ``ArrayList arr2 = ``new` `ArrayList<>(``            ``Arrays.asList(``5``, ``1``, ``4``, ``2``, ``6``, ``9``));` `        ``// Function Call``        ``System.out.println(maxMinProduct(arr1, arr2));``    ``}``}`

Output

`10`

Time Complexity: O(n), where n is the length of the array
Auxiliary Space: O(1)