1.
Introduction
1.1.
Problem Statement
1.2.
Sample Example
2.
Approach 1: Naive approach
2.1.
Steps of Algorithm
2.2.
Implementation in C++
2.3.
Implementation in Java
2.4.
Complexity analysis
3.
Approach 2: Efficient solution
3.1.
Steps of Algorithm
3.2.
Implementation in Java
3.3.
Complexity analysis
4.
4.1.
Can you declare an array without specifying its size?
4.2.
In Java, what are "jagged" arrays?
4.3.
We know that arrays are objects, why can't we use strArray.length()?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

# Product of maximum in first array and minimum in second

Shivani Singh
0 upvote
Roadmap to SDE career at Amazon
Speaker
Anubhav Sinha
SDE-2 @
25 Jun, 2024 @ 01:30 PM

## Introduction

Here in this blog, we are asked to see various approaches to find the product of the maximum element of the first array and the minimum element of the second array. We are already familiar with the array data structures and their various operations. So, let's delve a little deeper into this blog.

Source: DSA

### Problem Statement

In this problem, we are given two arrays of different lengths or maybe the same length. And we are asked to find the product of the maximum element of the first array and the minimum element of the second array. The problem statement is straightforward and simple; find the maximum element of the first array then find the minimum element of the second array and finally multiply both the elements. We can try different approaches to solve this problem. Let us understand the problem statement with the help of an example.

### Sample Example

Assume that the first array nums1 is: {2,3,4,5,6} and second array nums2 is: {3,6,8,9,5}.

As we can see from the array that the maximum element in nums1 is 6 and the minimum element in the second array is 3. After multiplying both the elements, we get the answer as 6 * 3 = 18.

The problem statement is quite clear with the above example.

In the above diagram, we can clearly see that the maximum element from the first array is 9 and the minimum element from the second array is 1. And we have to multiply both the numbers. So the result is 9.

Recommended topic, kth largest element in an array and  Euclid GCD Algorithm

## Approach 1: Naive approach

This approach is very simple to implement and straightforward. In this approach, we will first sort both arrays in ascending order. And after sorting the arrays, we already have an idea that in the first array, the maximum element will be at the last index, and for the second array, the minimum element will be at the first index. We simply have to return the products of both the indexâ€™s elements.

Source: Naive approach

Let us see its algorithm.

### Steps of Algorithm

Step 1: Sort both the arrays in ascending order using the inbuilt sort function or user-defined functions.

Step 2: After sorting both arrays, multiply the last element of nums1 i.e. the maximum element, and the first element of nums2 i.e. the minimum element.

Step 3: Return the multiply as the result. Let us see a diagram to understand this algorithm.

Let us see the implementation of this approach in the next section of this blog.

### Implementation in C++

``````#include <bits/stdc++.h>
using namespace std;
int MinMaxProduct(int nums1[],
int nums2[],
int n1,
int n2)
{
sort(nums1, nums1 + n1); //sorting the arrays
sort(nums2, nums2 + n2);
return nums1[n1 - 1] * nums2[0]; // Return product of maximum and minimum elements.
}
int main()
{
int nums1[100], nums2[100], n1, n2;
cout << "Enter Number of elements in array 1: ";
cin >> n1;
cout << "Enter element "<<endl;
for (int i = 0; i < n1; i++) {
cin >> nums1[i];
}
cout << "Enter Number of elements in array 2: ";
cin >> n2;
cout << "Enter element " <<endl;
for (int i = 0; i < n2; i++) {
cin >> nums2[i];
}
cout << "result of maximum and minimum elements is: "<<MinMaxProduct(nums1, nums2, n1, n2);
return 0;
}``````

Output:

``````Enter Number of elements in array 1: 4
Enter element
2 5 8 0
Enter Number of elements in array 2: 4
Enter element
2 0 1 5
result of maximum and minimum elements is: 0``````

### Implementation in Java

``````import java.util.*;
public class MinMaxProduct {
public static int Product(int nums1[], int nums2[], int n, int m) {
Arrays.sort(nums1); //inbuilt function to sort both the arrays
Arrays.sort(nums2);
return nums1[n - 1] * nums2[0]; //product of both the maximum and minimum elements
}
public static void main(String argc[]) {
int[] nums1 = new int[] {5,7,8,9,4,3};
int[] nums2 = new int[] {4,1,5,8,2,9};
int n = 6;
int m = 6;
System.out.println("product of maximum and minimum elements is: " + Product(nums1, nums2, n, m));
}
}``````

Output:

``product of maximum and minimum elements is: 9``

Let us analyze the time and complexity of this approach.

### Complexity analysis

Time complexity

This approach will take O(n log n ). Because in this approach, we are using the concept of sorting the array first in ascending order and returning the product of maximum and minimum elements of the arrays.

Space complexity

This will not cost any memory space. And it will take O(1).

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

## Approach 2: Efficient solution

In this approach, we will follow an efficient approach. We will traverse the whole array and then find the maximum and minimum elements respectively in both the arrays. And then we will simply multiply the maximum and minimum elements.

### Steps of Algorithm

Step 1: Traverse both the first and second arrays completely and finds their maximum and minimum elements.

Step 2: Find the product of maximum and minimum elements.

### Implementation in Java

``````import java.util.*;
public class Product
{
// Function to calculate the product
public static int minMaxProduct(int nums1[], int nums2[], int n1, int n2)
{
int max_element = nums1[0];
int min_element = nums2[0];
int i;
for (i = 1; i < n1 && i < n2; ++i)
{
if (nums1[i] > max_element) //maximum element of first array
max_element = nums1[i];
if (nums2[i] < min_element) //minimum element of second array
min_element = nums2[i];
}
while (i < n1)  //for remaining elements
{
if (nums1[i] > max_element)
max_element = nums1[i];
i++;
}
while (i < n2)
{
if (nums2[i] < min_element)
min_element = nums2[i];
i++;
}
return max_element * min_element;
}
public static void main(String argc[])
{
int[] nums1 = new int[] {10,11,13,2,5,6};
int[] nums2 = new int[] {4,6,7,9,2,4};
int n1 = 6;
int n2 = 6;
System.out.println("product of maximum and minimum element is: " + minMaxProduct(nums1, nums2, n1, n2));
}
}``````

Output:

``product of maximum and minimum element is: 26``

Now let us analyze the time and complexity of the above approach.

### Complexity analysis

Time complexity

The time complexity of this algorithm is O(N). Because here we are traversing the whole array and finding the maximum and minimum elements. And then returning its product.

Space complexity

The space complexity is O(1).

### Can you declare an array without specifying its size?

No, we cannot declare an array without first specifying its size. If we declare an array with no size, a compile-time error will occur.

### In Java, what are "jagged" arrays?

Arrays with Jagged Edges Arrays contain arrays of varying lengths. Multidimensional arrays are another name for jagged arrays.

### We know that arrays are objects, why can't we use strArray.length()?

Arrays, like classes in Java, are object references. Array can be used with Object methods such as function toString() { [native code] } () and hashCode (). Because the length is a data item in an array, it is not a method. That is why we use strArray.length.

## Conclusion

To conclude this blog, firstly we discussed the problem statement and different ways to solve the problems. For the first approach, we discussed its algorithm, pseudo-code, and complexities. And then we discussed another approach. We eventually saw how both the ways are different from each other.

Refer to our guided paths on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must have a look at the problemsinterview experiences, and interview bundle for placement preparations.

Recommended problems -

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass