Maximize sum of product of same-indexed elements of equal length subarrays obtained from two given arrays
Last Updated :
20 Jul, 2022
Given two arrays arr[] and brr[] of size N and M integers respectively, the task is to maximize the sum of the product of the same-indexed elements of two subarrays of an equal length with the selected subarray from the array brr[] being reversed.
Examples:
Input: arr[] = {-1, 3, -2, 4, 5}, brr[] = {4, -5}
Output: 26
Explanation:
Subarrays selected from the array arr[] and brr[] are {-2, 4} and {4, -5}.
Therefore, sum of the product of same-indexed elements = (-2)*(-5) + 4*4 = 26.
Input: arr[] = {1, 1, 1}, brr[] = {1, 1, 1}
Output: 3
Approach: The given problem can be solved by storing the product of each element from the two arrays in a 2D matrix and find the subarray with the maximum sum in each of the right diagonals of this 2D matrix which is similar to finding the maximum sum subarray in the array. Follow the steps below to solve the problem:
- Initialize a 2D matrix mat[][] of size N*M to store the product of each element from the two arrays.
- Generate all possible pairs from the given arrays arr[] and brr[] and store them in the 2D matrix mat[][].
- Now, for an equal length of the subarray, the idea is to traverse the diagonals of the matrix starting from the first row and first column.
- After every traversal of the diagonal store the elements in an array and then find the maximum sum subarray of the elements stored in the array.
- After the above steps, print the maximum sum among all the maximum sum obtained in the above steps.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to store product of each
// pair of elements from two arrays
void store_in_matrix(int a[], int b[],
int n1, int n2,
int mat[][10])
{
// Store product of pairs
// of elements in a matrix
for (int i = 0; i < n1; i++) {
for (int j = 0; j < n2; j++) {
mat[i][j] = (a[i] * b[j]);
}
}
}
// Function to find the maximum subarray
// sum in every right diagonal of the matrix
void maxsum_rt_diag(int n1, int n2,
int mat[][10], int& ans)
{
// Stores maximum continuous sum
int max_ending_here;
int i, j;
// Start with each element
// from the last column
for (int t = 0; t < n1; t++) {
i = t;
j = n2 - 1;
max_ending_here = 0;
// Check for each diagonal
while (i < n1 && j >= 0) {
max_ending_here = max_ending_here
+ mat[i][j];
i++;
j--;
// 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 (int t = 0; t < n2; t++) {
i = 0;
j = t;
max_ending_here = 0;
// Check for each diagonal
while (i < n1 && j >= 0) {
max_ending_here = max_ending_here
+ mat[i][j];
i++;
j--;
// 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;
}
}
}
// Function to initialize matrix to 0
void initMatrix(int mat[10][10],
int n1, int n2)
{
// Traverse each row
for (int i = 0; i < n1; i++) {
// Traverse each column
for (int j = 0; j < n2; j++) {
mat[i][j] = 0;
}
}
}
// Function to find the maximum sum of
// the two equal subarray selected from
// the given two arrays a[] and b[]
void findMaxProduct(int a[], int n1,
int b[], int n2)
{
// Stores the matrix
int mat[10][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
int ans = 0;
// Find maximum subarray sum in
// every right diagonal of matrix
maxsum_rt_diag(n1, n2, mat, ans);
// Print the maximum sum
cout << ans << "\n";
}
// Driver Code
int main()
{
// Initialize two arrays
int arr[] = { -1, 3, -2, 4, 5 };
int brr[] = { 4, -5 };
// Find size of array
int N = sizeof(arr) / sizeof(arr[0]);
int M = sizeof(brr) / sizeof(brr[0]);
// Function Call
findMaxProduct(arr, N, brr, M);
return 0;
}
Java
// java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
public class GFG {
// Function to store product of each
// pair of elements from two arrays
static void store_in_matrix(int a[], int b[], int n1,
int n2, int mat[][])
{
// Store product of pairs
// of elements in a matrix
for (int i = 0; i < n1; i++) {
for (int j = 0; j < n2; j++) {
mat[i][j] = (a[i] * b[j]);
}
}
}
// Function to find the maximum subarray
// sum in every right diagonal of the matrix
static int maxsum_rt_diag(int n1, int n2, int mat[][])
{
// Stores maximum continuous sum
int max_ending_here;
int i, j;
// Stores the result
int ans = 0;
// Start with each element
// from the last column
for (int t = 0; t < n1; t++) {
i = t;
j = n2 - 1;
max_ending_here = 0;
// Check for each diagonal
while (i < n1 && j >= 0) {
max_ending_here
= max_ending_here + mat[i][j];
i++;
j--;
// 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 (int t = 0; t < n2; t++) {
i = 0;
j = t;
max_ending_here = 0;
// Check for each diagonal
while (i < n1 && j >= 0) {
max_ending_here
= max_ending_here + mat[i][j];
i++;
j--;
// 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 find the maximum sum of
// the two equal subarray selected from
// the given two arrays a[] and b[]
static void findMaxProduct(int a[], int n1, int b[],
int n2)
{
// Stores the matrix
int mat[][] = new int[10][10];
// Store product of each element
// from two arrays in a matrix
store_in_matrix(a, b, n1, n2, mat);
// Stores the result
int ans = 0;
// Find maximum subarray sum in
// every right diagonal of matrix
ans = maxsum_rt_diag(n1, n2, mat);
// Print the maximum sum
System.out.println(ans);
}
// Driver Code
public static void main(String[] args)
{
// Initialize two arrays
int arr[] = { -1, 3, -2, 4, 5 };
int brr[] = { 4, -5 };
// Find size of array
int N = arr.length;
int M = brr.length;
// Function Call
findMaxProduct(arr, N, brr, M);
}
}
// This code is contributed by Kingash.
Python3
# 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.
C#
// C# program for the above approach
using System;
public class GFG {
// Function to store product of each
// pair of elements from two arrays
static void store_in_matrix(int[] a, int[] b, int n1,
int n2, int[, ] mat)
{
// Store product of pairs
// of elements in a matrix
for (int i = 0; i < n1; i++) {
for (int j = 0; j < n2; j++) {
mat[i, j] = (a[i] * b[j]);
}
}
}
// Function to find the maximum subarray
// sum in every right diagonal of the matrix
static int maxsum_rt_diag(int n1, int n2, int[, ] mat)
{
// Stores maximum continuous sum
int max_ending_here;
int i, j;
// Stores the result
int ans = 0;
// Start with each element
// from the last column
for (int t = 0; t < n1; t++) {
i = t;
j = n2 - 1;
max_ending_here = 0;
// Check for each diagonal
while (i < n1 && j >= 0) {
max_ending_here
= max_ending_here + mat[i, j];
i++;
j--;
// 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 (int t = 0; t < n2; t++) {
i = 0;
j = t;
max_ending_here = 0;
// Check for each diagonal
while (i < n1 && j >= 0) {
max_ending_here
= max_ending_here + mat[i, j];
i++;
j--;
// 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 find the maximum sum of
// the two equal subarray selected from
// the given two arrays a[] and b[]
static void findMaxProduct(int[] a, int n1, int[] b,
int n2)
{
// Stores the matrix
int[, ] mat = new int[10, 10];
// Store product of each element
// from two arrays in a matrix
store_in_matrix(a, b, n1, n2, mat);
// Stores the result
int ans = 0;
// Find maximum subarray sum in
// every right diagonal of matrix
ans = maxsum_rt_diag(n1, n2, mat);
// Print the maximum sum
Console.WriteLine(ans);
}
// Driver Code
public static void Main(string[] args)
{
// Initialize two arrays
int[] arr = { -1, 3, -2, 4, 5 };
int[] brr = { 4, -5 };
// Find size of array
int N = arr.Length;
int M = brr.Length;
// Function Call
findMaxProduct(arr, N, brr, M);
}
}
// This code is contributed by ukasp.
JavaScript
<script>
// Javascript program implementation
// of the approach
// Function to store product of each
// pair of elements from two arrays
function store_in_matrix(a, b, n1,
n2, mat)
{
// Store product of pairs
// of elements in a matrix
for (let i = 0; i < n1; i++) {
for (let j = 0; j < n2; j++) {
mat[i][j] = (a[i] * b[j]);
}
}
}
// Function to find the maximum subarray
// sum in every right diagonal of the matrix
function maxsum_rt_diag(n1, n2, mat)
{
// Stores maximum continuous sum
let max_ending_here;
let i, j;
// Stores the result
let ans = 0;
// Start with each element
// from the last column
for (let t = 0; t < n1; t++) {
i = t;
j = n2 - 1;
max_ending_here = 0;
// Check for each diagonal
while (i < n1 && j >= 0) {
max_ending_here
= max_ending_here + mat[i][j];
i++;
j--;
// 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 (let t = 0; t < n2; t++) {
i = 0;
j = t;
max_ending_here = 0;
// Check for each diagonal
while (i < n1 && j >= 0) {
max_ending_here
= max_ending_here + mat[i][j];
i++;
j--;
// 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 find the maximum sum of
// the two equal subarray selected from
// the given two arrays a[] and b[]
function findMaxProduct(a, n1, b,
n2)
{
// Stores the matrix
let mat = new Array(10);
for (var i = 0; i < mat.length; i++) {
mat[i] = new Array(2);
}
// Store product of each element
// from two arrays in a matrix
store_in_matrix(a, b, n1, n2, mat);
// Stores the result
let ans = 0;
// Find maximum subarray sum in
// every right diagonal of matrix
ans = maxsum_rt_diag(n1, n2, mat);
// Print the maximum sum
document.write(ans);
}
// Driver Code
// Initialize two arrays
let arr = [ -1, 3, -2, 4, 5 ];
let brr = [ 4, -5 ];
// Find size of array
let N = arr.length;
let M = brr.length;
// Function Call
findMaxProduct(arr, N, brr, M);
// This code is contributed by souravghosh0416.
</script>
Time Complexity: O(N2)
Auxiliary Space: O(N2), since N2 extra space has been taken.
Similar Reads
Minimize the Sum of all the subarrays made up of the products of same-indexed elements Given two arrays arr[] and arr2[] of length N, the task is to find the minimum sum of all the subarrays made up of the products of the same indexed elements of both the arrays after rearranging the second array. Note: Since the answer can be very large, print the answer modulo 109 + 7. Examples: Inp
7 min read
Maximise sum of product of pairs by choosing subsequence of same length from given Arrays Given two integer arrays A[] and B[] of different length N and M respectively, the task is to choose any subsequence of same length from each array such that sum of product of pairs at corresponding indices in the subsequence is maximised. Example: Input: A = {4, -1, -3, 3}, B = {-4, 0, 5}Output: 27
15+ min read
Maximize score of same-indexed subarrays selected from two given arrays Given two arrays A[] and B[], both consisting of N positive integers, the task is to find the maximum score among all possible same-indexed subarrays in both the arrays such that the score of any subarray over the range [L, R] is calculated by the maximum of the values (AL*BL + AL + 1*BL + 1 + ... +
15+ min read
Maximum length of same indexed subarrays from two given arrays satisfying the given condition Given two arrays arr[] and brr[] and an integer C, the task is to find the maximum possible length, say K, of the same indexed subarrays such that the sum of the maximum element in the K-length subarray in brr[] with the product between K and sum of the K-length subarray in arr[] does not exceed C.
15+ min read
Maximize product obtained by taking one element from each array of a given list Given a list arr[] consisting of arrays of varying lengths, the task is to find the maximum product that can be formed by taking exactly one element from each array present in the list. Examples: Input: arr[] = {{-3, -4}, {1, 2, -3}}Output: 12Explanation: Pick -4 from the first array and -3 from the
15 min read
Maximum length of subarray consisting of same type of element on both halves of sub-array Given an array arr[] of N integers, the task is to find the maximum length of sub-array consisting of the same type of element on both halves of the sub-array. Also, the elements on both halves differ from each other. Examples: Input: arr[] = {2, 3, 4, 4, 5, 5, 6, 7, 8, 10}Output: 4Explanation:{2, 3
8 min read
Maximum length of subarray with same sum at corresponding indices from two Arrays Given two arrays A[] and B[] both consisting of N integers, the task is to find the maximum length of subarray [i, j] such that the sum of A[i... j] is equal to B[i... j]. Examples: Input: A[] = {1, 1, 0, 1}, B[] = {0, 1, 1, 0}Output: 3Explanation: For (i, j) = (0, 2), sum of A[0... 2] = sum of B[0.
6 min read
Maximize product of two closest numbers of other array for every element in given array Given arrays arr1[] of size M and arr2[] of size N having length at least 2, the task is for every element in arr1[], maximize the product of two elements in arr2[] which are closest to the element in arr1[]. The closest elements must be present on distinct indices. Example: Input: arr1 = [5, 10, 17
15 min read
Maximum product of sum of two contiguous subarrays of an array Given an array arr[] of N positive integers, the task is to split the array into two contiguous subarrays such that the product of the sum of two contiguous subarrays is maximum. Examples: Input: arr[] = {4, 10, 1, 7, 2, 9} Output: 270 All possible partitions and their product of sum are: {4} and {1
10 min read
Maximize sum of products at corresponding indices in two arrays by reversing at most one subarray in first Array Given two arrays A and B of size N, the task is to maximize the sum of A[i]*B[i] across all values of i from 0 to N-1 by reversing at most one subarray of array A. Examples: Input: N = 4, A = [5, 1, 2, 3], B = [1, 4, 3, 2]Output: 33Explanation: Array A after reversing the subarray A[0, 1] will becom
15 min read