# Python3 program for the above approach
# Function to store product of each
# pair of elements from two arrays
def store_in_matrix(a, b, n1, n2, mat):
# Store product of pairs
# of elements in a matrix
for i in range(n1):
for j in range(n2):
mat[i][j] = (a[i] * b[j])
# Function to find the maximum subarray
# sum in every right diagonal of the matrix
def maxsum_rt_diag(n1, n2, mat, ans):
# Stores maximum continuous sum
max_ending_here=0
i, j = 0, 0
# Start with each element
# from the last column
for t in range(n1):
i = t
j = n2 - 1
max_ending_here = 0
# Check for each diagonal
while (i < n1 and j >= 0):
max_ending_here = max_ending_here + mat[i][j]
i += 1
j -= 1
# Update ans if max_ending_here
# is greater than ans
if (ans < max_ending_here):
ans = max_ending_here
# If max_ending_here is -ve
if (max_ending_here < 0):
# Reset it to 0
max_ending_here = 0
# Start with each element
# from the first row
for t in range(n2):
i = 0
j = t
max_ending_here = 0
# Check for each diagonal
while (i < n1 and j >= 0):
max_ending_here = max_ending_here + mat[i][j]
i += 1
j -= 1
# Update ans if max_ending_here
# is greater than ans
if (ans < max_ending_here):
ans = max_ending_here
# If max_ending_here is -ve
if (max_ending_here < 0):
# Reset to 0
max_ending_here = 0
return ans
# Function to initialize matrix to 0
def initMatrix(mat, n1, n2):
# Traverse each row
for i in range(n1):
# Traverse each column
for j in range(n2):
mat[i][j] = 0
# Function to find the maximum sum of
# the two equal subarray selected from
# the given two arrays a[] and b[]
def findMaxProduct(a, n1, b, n2):
# Stores the matrix
mat = [[ 0 for i in range(10)] for i in range(10)]
# Initialize each element in mat[] to 0
initMatrix(mat, n1, n2)
# Store product of each element
# from two arrays in a matrix
store_in_matrix(a, b, n1, n2, mat)
# Stores the result
ans = 0
# Find maximum subarray sum in
# every right diagonal of matrix
ans = maxsum_rt_diag(n1, n2, mat, ans)
# Print the maximum sum
print (ans)
# Driver Code
if __name__ == '__main__':
# Initialize two arrays
arr= [-1, 3, -2, 4, 5]
brr= [4, -5]
# Find size of array
N = len(arr)
M = len(brr)
# Function Call
findMaxProduct(arr, N, brr, M)
# This code is contributed by mohit kumar 29.