Home »
Data Structure »
Array Data Structure
Product of maximum in the first array and minimum in second
C++ implementation of the product of maximum in the first array and minimum in second.
Submitted by Vikneshwar GK, on January 16, 2022
The aim of this problem statement is to find the product of two elements under certain conditions. The conditions are that one element should be the maximum in the first array and the other element should be the minimum in the second array.
Example:
Input:
test1[] = {4, 0, 1, 8, 3, 5, 7}
test2[] = {0, 7, 9, 1, 2, -2, 10}
Output:
-16
Explanation:
The maximum element in test1 is 8 and the minimum element
in test2 is -2. So the product of these items is -16.
Input:
test1[] = {4, 8, 10, 6, 5, 10}
test2[] = {2, 0, 4, 8, 9, 3, 7}
Output:
0
Explanation:
The maximum element in test1 is 10 and the minimum element
in test2 is 0. So the product of these items is 0.
Solution 1: Sorting and Solving
This method of approach involves sorting both the arrays in either ascending or descending order. After sorting, we can easily find the max and min elements in the first and second arrays, respectively. Then we find the product and display the result.
C++ Implementation:
// Program to calculate the product to two elements
// first element max in first array
// second element min in second array
#include <bits/stdc++.h>
using namespace std;
// calculate the product of two elements
int calProduct(int array1[],
int array2[],
int size1,
int size2)
{
// sort the arrays
sort(array1, array1 + size1);
sort(array2, array2 + size2);
// Return product of
// maximum and minimum.
return array1[size1 - 1] * array2[0];
}
// main function
int main()
{
int array1[100], array2[100], size1, size2;
cout << "Enter Number of elements in array 1: ";
cin >> size1;
for (int i = 0; i < size1; i++) {
cout << "Enter element " << i + 1 << ":";
cin >> array1[i];
}
cout << "Enter Number of elements in array 2: ";
cin >> size2;
for (int i = 0; i < size2; i++) {
cout << "Enter element " << i + 1 << ":";
cin >> array2[i];
}
cout << calProduct(array1, array2, size1, size2);
return 0;
}
Output:
Enter Number of elements in array 1: 3
Enter element 1:5
Enter element 2:1
Enter element 3:0
Enter Number of elements in array 2: 4
Enter element 1:4
Enter element 2:3
Enter element 3:2
Enter element 4:7
10
Time Complexity: O(nlog(n))
Solution 2: Find Maximum and Minimum by comparing elements
In this approach, we simply traverse both the arrays and find the minimum and maximum by comparing the adjacent elements.
C++ Implementation:
// Program to calculate the product to two elements
// first element max in first array
// second element min in second array
#include <bits/stdc++.h>
using namespace std;
// calculate product to two elements
int calProduct(int array1[], int array2[],
int size1, int size2){
// Assign first element of array 1 to max
int max = array1[0];
// Assign first element of array 2 to min
int min = array2[0];
int i;
for (i = 1; i < size1 && i < size2; ++i) {
// When current element is greater than max
// assign current element to max
if (array1[i] > max)
max = array1[i];
// When current element is lesser than min
// assign current element to min
if (array2[i] < min)
min = array2[i];
}
// Iterate for remaining elements in array1
while (i < size1) {
if (array1[i] > max)
max = array1[i];
i++;
}
// Iterate for remaining elements in array2
while (i < size2) {
if (array2[i] < min)
min = array2[i];
i++;
}
return max * min;
}
// main function
int main()
{
int array1[100], array2[100], size1, size2;
cout << "Enter Number of elements in array 1: ";
cin >> size1;
for (int i = 0; i < size1; i++) {
cout << "Enter element " << i + 1 << ":";
cin >> array1[i];
}
cout << "Enter Number of elements in array 2: ";
cin >> size2;
for (int i = 0; i < size2; i++) {
cout << "Enter element " << i + 1 << ":";
cin >> array2[i];
}
cout << calProduct(array1, array2, size1, size2);
return 0;
}
Output:
Enter Number of elements in array 1: 5
Enter element 1:7
Enter element 2:4
Enter element 3:3
Enter element 4:9
Enter element 5:4
Enter Number of elements in array 2: 3
Enter element 1:8
Enter element 2:4
Enter element 3:3
27
Time Complexity: O(n)