原文:
给定两个数组,任务是计算第一个数组的最大元素和第二个数组的最小元素的乘积。
参考文献:在 adobe 中提问(来源:)。
示例:
input : arr1[] = {5, 7, 9, 3, 6, 2},
arr2[] = {1, 2, 6, -1, 0, 9}
output : max element in first array
is 9 and min element in second array
is -1\. the product of these two is -9.
input : arr1[] = {1, 4, 2, 3, 10, 2},
arr2[] = {4, 2, 6, 5, 2, 9}
output : max element in first array
is 10 and min element in second array
is 2\. the product of these two is 20.
方法 1 - 简单方法:我们首先对两个数组进行排序。 然后,我们可以轻松地在第一个数组中找到最大值,在第二个数组中找到最小值。 最后,我们返回最小值和最大值的乘积。
c
// c program to calculate the
// product of max element of
// first array and min element
// of second array
#include
using namespace std;
// function to calculate
// the product
int minmaxproduct(int arr1[],
int arr2[],
int n1,
int n2)
{
// sort the arrays to find
// the maximum and minimum
// elements in given arrays
sort(arr1, arr1 n1);
sort(arr2, arr2 n2);
// return product of
// maximum and minimum.
return arr1[n1 - 1] * arr2[0];
}
// driven code
int main()
{
int arr1[] = { 10, 2, 3, 6, 4, 1 };
int arr2[] = { 5, 1, 4, 2, 6, 9 };
int n1 = sizeof(arr1) / sizeof(arr1[0]);
int n2 = sizeof(arr1) / sizeof(arr1[0]);
cout << minmaxproductt(arr1, arr2, n1, n2);
return 0;
}
java
// java program to find the
// to calculate the product
// of max element of first
// array and min element of
// second array
import java.util.*;
import java.lang.*;
class gfg
{
// function to calculate
// the product
public static int minmaxproduct(int arr1[],
int arr2[],
int n1,
int n2)
{
// sort the arrays to find the
// maximum and minimum elements
// in given arrays
arrays.sort(arr1);
arrays.sort(arr2);
// return product of maximum
// and minimum.
return arr1[n1 - 1] * arr2[0];
}
// driver code
public static void main(string argc[])
{
int [] arr1= new int []{ 10, 2, 3,
6, 4, 1 };
int [] arr2 = new int []{ 5, 1, 4,
2, 6, 9 };
int n1 = 6;
int n2 = 6;
system.out.println(minmaxproduct(arr1,
arr2,
n1, n2));
}
}
/*this code is contributed by sagar shukla.*/
python
# a python program to find the to
# calculate the product of max
# element of first array and min
# element of second array
# function to calculate the product
def minmaxproduct(arr1, arr2, n1, n2):
# sort the arrays to find the
# maximum and minimum elements
# in given arrays
arr1.sort()
arr2.sort()
# return product of maximum
# and minimum.
return arr1[n1 - 1] * arr2[0]
# driver program
arr1 = [10, 2, 3, 6, 4, 1]
arr2 = [5, 1, 4, 2, 6, 9]
n1 = len(arr1)
n2 = len(arr2)
print(minmaxproduct(arr1, arr2, n1, n2))
# this code is contributed by shrikant13\.
c#
// c# program to find the to
// calculate the product of
// max element of first array
// and min element of second array
using system;
class gfg
{
// function to calculate the product
public static int minmaxproduct(int []arr1,
int []arr2,
int n1,
int n2)
{
// sort the arrays to find the
// maximum and minimum elements
// in given arrays
array.sort(arr1);
array.sort(arr2);
// return product of maximum
// and minimum.
return arr1[n1 - 1] * arr2[0];
}
// driver code
public static void main()
{
int [] arr1= new int []{ 10, 2, 3,
6, 4, 1 };
int [] arr2 = new int []{ 5, 1, 4,
2, 6, 9 };
int n1 = 6;
int n2 = 6;
console.writeline(minmaxproduct(arr1, arr2,
n1, n2));
}
}
/*this code is contributed by vt_m.*/
php
输出:
10
时间复杂度:o(n log n)
。
空间复杂度:o(1)
。
方法 2 - 高效方法:在这种方法中,我们只需遍历整个数组,然后在第一个数组中找到最大值,在第二个数组中找到最小值,就可以轻松获得最小值和最大值的乘积。
c
// c program to find the to
// calculate the product of
// max element of first array
// and min element of second array
#include
using namespace std;
// function to calculate the product
int minmaxproduct(int arr1[], int arr2[],
int n1, int n2)
{
// initialize max of first array
int max = arr1[0];
// initialize min of second array
int min = arr2[0];
int i;
for (i = 1; i < n1 && i < n2; i)
{
// to find the maximum
// element in first array
if (arr1[i] > max)
max = arr1[i];
// to find the minimum
// element in second array
if (arr2[i] < min)
min = arr2[i];
}
// process remaining elements
while (i < n1)
{
if (arr1[i] > max)
max = arr1[i];
i ;
}
while (i < n2)
{
if (arr2[i] < min)
min = arr2[i];
i ;
}
return max * min;
}
// driven code
int main()
{
int arr1[] = { 10, 2, 3, 6, 4, 1 };
int arr2[] = { 5, 1, 4, 2, 6, 9 };
int n1 = sizeof(arr1) / sizeof(arr1[0]);
int n2 = sizeof(arr1) / sizeof(arr1[0]);
cout << minmaxproduct(arr1, arr2, n1, n2)
<< endl;
return 0;
}
java
// java program to calculate the
// product of max element of first
// array and min element of second array
import java.util.*;
import java.lang.*;
class gfg
{
// function to calculate the product
public static int minmaxproduct(int arr1[],
int arr2[],
int n1,
int n2)
{
// initialize max of
// first array
int max = arr1[0];
// initialize min of
// second array
int min = arr2[0];
int i;
for (i = 1; i < n1 && i < n2; i)
{
// to find the maximum
// element in first array
if (arr1[i] > max)
max = arr1[i];
// to find the minimum element
// in second array
if (arr2[i] < min)
min = arr2[i];
}
// process remaining elements
while (i < n1)
{
if (arr1[i] > max)
max = arr1[i];
i ;
}
while (i < n2)
{
if (arr2[i] < min)
min = arr2[i];
i ;
}
return max * min;
}
// driver code
public static void main(string argc[])
{
int [] arr1= new int []{ 10, 2, 3,
6, 4, 1 };
int [] arr2 = new int []{ 5, 1, 4,
2, 6, 9 };
int n1 = 6;
int n2 = 6;
system.out.println(minmaxproduct(arr1, arr2,
n1, n2));
}
}
// this code is contributed by sagar shukla
python3
# python3 program to find the to
# calculate the product of
# max element of first array
# and min element of second array
# function to calculate the product
def minmaxproduct(arr1, arr2,
n1, n2) :
# initialize max of first array
max = arr1[0]
# initialize min of second array
min = arr2[0]
i = 1
while (i < n1 and i < n2) :
# to find the maximum
# element in first array
if (arr1[i] > max) :
max = arr1[i]
# to find the minimum
# element in second array
if (arr2[i] < min) :
min = arr2[i]
i = 1
# process remaining elements
while (i < n1) :
if (arr1[i] > max) :
max = arr1[i]
i = 1
while (i < n2):
if (arr2[i] < min) :
min = arr2[i]
i = 1
return max * min
# driver code
arr1 = [10, 2, 3, 6, 4, 1 ]
arr2 = [5, 1, 4, 2, 6, 9 ]
n1 = len(arr1)
n2 = len(arr1)
print(minmaxproduct(arr1, arr2, n1, n2))
# this code is contributed by smitha
c
// c# program to find the to
// calculate the product of
// max element of first array
// and min element of second array
using system;
class gfg
{
// function to calculate
// the product
public static int minmaxproduct(int []arr1,
int []arr2,
int n1,
int n2)
{
// initialize max of
// first array
int max = arr1[0];
// initialize min of
// second array
int min = arr2[0];
int i;
for (i = 1; i < n1 && i < n2; i)
{
// to find the maximum element
// in first array
if (arr1[i] > max)
max = arr1[i];
// to find the minimum element
// in second array
if (arr2[i] < min)
min = arr2[i];
}
// process remaining elements
while (i < n1)
{
if (arr1[i] > max)
max = arr1[i];
i ;
}
while (i < n2)
{
if (arr2[i] < min)
min = arr2[i];
i ;
}
return max * min;
}
// driver code
public static void main()
{
int [] arr1= new int []{ 10, 2, 3,
6, 4, 1 };
int [] arr2 = new int []{ 5, 1, 4,
2, 6, 9 };
int n1 = 6;
int n2 = 6;
console.writeline(minmaxproduct(arr1, arr2,
n1, n2));
}
}
// this code is contributed by vt_m
php
$max)
$max = $arr1[$i];
// to find the minimum element
// in second array
if ($arr2[$i] < $min)
$min = $arr2[$i];
}
// process remaining elements
while ($i < $n1)
{
if ($arr1[$i] > $max)
$max = $arr1[$i];
$i ;
}
while ($i < $n2)
{
if ($arr2[$i] < $min)
$min = $arr2[$i];
$i ;
}
return $max * $min;
}
// driven code
$arr1 = array(10, 2, 3,
6, 4, 1);
$arr2 = array(5, 1, 4,
2, 6, 9);
$n1 = count($arr1);
$n2 = count($arr2);
echo minmaxproduct($arr1, $arr2,
$n1, $n2);
// this code is contributed by anuj_67.
?>
输出:
10
时间复杂度:o(n)
。
空间复杂度:o(1)
。
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处