给定两个分别大小分别为n1和n2的数组arr1 []和arr2 [],我们必须找到第一个数组arr1 []的最大元素与第二个数组arr2 []的最小元素的乘积。
就像我们在arr1 [] = {5,1,6,8,9}和arr2 [] = {2,9,8,5,3}中有元素一样,因此arr1中的最大元素为9而在arr1中的最小元素为arr2是2,所以两者的乘积是9 * 2 = 18,同样,我们必须编写一个程序来解决给定的问题。
输入项
arr1[] = {6, 2, 5, 4, 1} arr2[] = {3, 7, 5, 9, 6}
输出结果
18
说明
MAX(arr1) * MIN(arr2) → 6 * 3 = 18
输入项
arr1[] = { 2, 3, 9, 11, 1 } arr2[] = { 5, 4, 2, 6, 9 }
输出结果
22
说明
MAX(arr1) * MIN(arr2) → 11 * 2 = 22
我们将两个数组arr1和arr2作为输入
我们将按照升序对两个数组进行排序。
我们将把arr1的最后一个元素(最大元素)和arr2的第一个元素(最小元素)相乘。
返回。
Start In function int sortarr(int arr[], int n) Step 1→ Declare and initialize temp Step 2→ For i = 0 and i < n-1 and ++i For j = i+1 and j<n and j++ If arr[i]> arr[j] then, Set temp as arr[i] Set arr[i] as arr[j] Set arr[j] as temp In Function int minMaxProduct(int arr1[], int arr2[], int n1, int n2) Step 1→ Call sortarr(arr1, n1) Step 2→ Call sortarr(arr2, n2) Step 3→ Return (arr1[n1 - 1] * arr2[0]) In Function int main() Step 1→ Declare and Initialize arr1[] = { 2, 3, 9, 11, 1 } Step 2→ Declare and Initialize arr2[] = { 5, 4, 2, 6, 9 } Step 3→ Declare and Initialize n1, n2 and initialize the size of both arrays Step 4→ Print minMaxProduct (arr1, arr2, n1, n2)) Stop
#include <stdio.h> int sortarr(int arr[], int n){ int temp; for (int i = 0; i < n-1; ++i){ for(int j = i+1; j<n; j++){ if(arr[i]> arr[j]){ temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } return 0; } int minMaxProduct(int arr1[], int arr2[], int n1, int n2){ // Sort the arrays to get // maximum and minimum sortarr(arr1, n1); sortarr(arr2, n2); // Return product of // maximum and minimum. return arr1[n1 - 1] * arr2[0]; } int main(){ int arr1[] = { 2, 3, 9, 11, 1 }; int arr2[] = { 5, 4, 2, 6, 9 }; int n1 = sizeof(arr1) / sizeof(arr1[0]); int n2 = sizeof(arr1) / sizeof(arr1[0]); printf("%d\n",minMaxProduct (arr1, arr2, n1, n2)); return 0; }
输出结果
如果运行上面的代码,它将生成以下输出-
22