Skip to main content

Product of Maximum in First Array and Minimum in Second

Problem​

Given two arrays of A and B respectively of sizes N1 and N2, the task is to calculate the product of the maximum element of the first array and minimum element of the second array.

Examples:​

Example 1:

Input : A[] = {5, 7, 9, 3, 6, 2}, 
B[] = {1, 2, 6, -1, 0, 9}
Output : -9
Explanation:
The first array is 5 7 9 3 6 2.
The max element among these elements is 9.
The second array is 1 2 6 -1 0 9.
The min element among these elements is -1.
The product of 9 and -1 is 9*-1=-9.

Example 2:

Input : A[] = {0, 0, 0, 0}, 
B[] = {1, -1, 2}
Output : 0

Your task:​

This is a function problem. The input is already taken care of by the driver code. You only need to complete the function find_multiplication() that takes an array of integer (A), another array of integer (B), size of array A(n), size of array B(m) and return the product of the max element of the first array and the minimum element of the second array. The driver code takes care of the printing.

  • Expected Time Complexity: O(N+M)O(N+M)
  • Expected Auxiliary Space: O(1)O(1)

Constraints:​

  • 1≀N,M≀1061 ≀ N, M ≀ 10^6
  • βˆ’108≀Ai,Bi≀108-10^8 ≀ Ai, Bi ≀ 10^8

Solution​

Python​

def find_multiplication (self, arr, brr, n, m) : 
max_arr = arr[0]
min_brr = brr[0]
for i in range(1, n):
if arr[i] > max_arr:
max_arr = arr[i]
for i in range(1, m):
if brr[i] < min_brr:
min_brr = brr[i]
return max_arr * min_brr

Java​

public static long find_multiplication (int arr[], int brr[], int n, int m) {
int maxArr = arr[0];
int minBrr = brr[0];
for (int i = 1; i < n; ++i) {
if (arr[i] > maxArr) {
maxArr = arr[i];
}
}
for (int i = 1; i < m; ++i) {
if (brr[i] < minBrr) {
minBrr = brr[i];
}
}
return (long) maxArr * minBrr;
}

C++​

long long find_multiplication(int a[], int b[], int n, int m) {
int max_a = INT_MIN;
int min_b = INT_MAX;
for (int i = 0; i < n; ++i) {
if (a[i] > max_a) {
max_a = a[i];
}
}
for (int i = 0; i < m; ++i) {
if (b[i] < min_b) {
min_b = b[i];
}
}
return (long long) max_a * min_b;
}

C​

long long find_multiplication(int a[], int b[], int n, int m) {
int max_a = INT_MIN;
int min_b = INT_MAX;
for (int i = 0; i < n; ++i) {
if (a[i] > max_a) {
max_a = a[i];
}
}
for (int i = 0; i < m; ++i) {
if (b[i] < min_b) {
min_b = b[i];
}
}
return (long long) max_a * min_b;
}
  • Time Complexity: O(N+M)O(N+M)
  • Auxiliary Space: O(1)O(1)