Examples:
1. Input:
arr1[ ] = {4, 5, 1, 9, 3, 5, 7}
arr2[ ] = {0, 7,3, 5, 2, -3, 10}
Output:
-27
Here
The maximum element in the arr1 is 9 and the minimum element
in arr2 is -3.
So the product of these items is -27.
2. Input:
arr1[ ] = {4, 5, 1, 9, 3, 5, 7}
arr2[ ] = {0, 7,3, 5, 2, 5, 10}
Output:
0
Here
The maximum element in the arr1 is 9 and the minimum element
in arr2 is 0.
So the product of these items is 0.
#include <bits/stdc++.h>
using namespace std;
// calculate product to two elements
int calProduct(int arr1[], int arr2[],int n1, int n2)
{
// Assign first element of array 1 to max
int max = arr1[0];
// Assign first element of array 2 to min
int min = arr2[0];
int i;
for (i = 1; i < n1 && i < n2; ++i)
{
/// max arr1
if (arr1[i] > max)
max = arr1[i];
// When current element is lesser than min
// assign current element to min
if (arr2[i] < min)
min = arr2[i];
}
//min form arr2
while (i < n1)
{
if (arr1[i] > max)
max = arr1[i];
i++;
}
// Iterate for remaining elements in arr2
while (i < n2)
{
if (arr2[i] < min)
min = arr2[i];
i++;
}
return max * min;
}
// main function
int main()
{
int arr1[100], arr2[100], n1, n2;
cout << "Enter Number of elements in arr1: ";
cin >> n1;
for (int i = 0; i < n1; i++)
{
cin >> arr1[i];
}
cout << "Enter Number of elements in arr2: ";
cin >> n2;
for (int i = 0; i < n2; i++)
{
cin >> arr2[i];
}
cout << calProduct(arr1, arr2, n1, n2);
return 0;
}
Output:
Enter Number of elements in arr1: 5
1 2 3 4 5
Enter Number of elements in arr2: 5
6 7 8 9 10
30
0 Comments