1 | initial version |
To obtain the highest sum that can be obtained by multiplying the corresponding elements of two arrays in Python, you can use the following code:
array1 = [1, 2, 3, 4]
array2 = [5, 6, 7, 8]
result = sum(max(i * j for j in array2) for i in array1)
print(result) # Output: 64
This code first initializes two arrays, array1
and array2
, which are to be multiplied to obtain the highest sum. The sum
function is then used with a generator expression to compute the sum of the maximum product of the corresponding elements. Specifically, the generator expression first computes the product of each element of array1
with the maximum element of array2
using the max
function and then finds the maximum of these products using the max
function. Finally, the sum
function adds up all these maximum products to obtain the highest sum.