Skip to content
Related Articles

Related Articles

Maximize sum of product of same-indexed elements of equal length subarrays obtained from two given arrays
  • Last Updated : 03 May, 2021

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.

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.
Output: 
26

 

Time Complexity: O(N2)
Auxiliary Space: O(N2)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :