Maximize product of same-indexed elements of same size subsequences

Given two integer arrays a[] and b[], the task is to find the maximum possible product of the same indexed elements of two equal length subsequences from the two given arrays.

Examples:

Input: a[] = {-3, 1, -12, 7}, b[] = {3, 2, -6, 7}
Output: 124
Explanation: The subsequence [1, -12, 7] from a[] and subsequence [3, -6, 7] from b[] gives the maximum scaler product, (1*3 + (-12)*(-6) + 7*7) = 124.

Input: a[] = {-2, 6, -2, -5}, b[] = {-3, 4, -2, 8}
Output: 54
Explanation: The subsequence [-2, 6] from a[] and subsequence [-3, 8] from b[] gives the maximum scaler product, ((-2)*(-3) + 6*8) = 54.

Approach: The problem is similar to the Longest Common Subsequence problem of Dynamic Programming. A Recursive and Memoization based Top-Down method is explained below:



  • Let us define a function F(X, Y), which returns the maximum scalar product between the arrays a[0-X] and b[0-Y].
  • Following is the recursive definition:

    F(X, Y) = max(a[X] * b[Y] + F(X – 1, Y – 1); Use the last elements from both arrays and check further

    a[X] * b[Y]; Only use the last element as the maximum product

    F(X – 1, Y); Ignore the last element from the first array

    F(X, Y – 1)); Ignore the last element from the second element

  • Use a 2-D array for memoization and to avoid recomputation of same sub-problems.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to maximize
// product of same-indexed elements
// of same size subsequences
  
#include <bits/stdc++.h>
using namespace std;
  
#define INF 10000000
  
// Utility function to find the maximum
int maximum(int A, int B, int C, int D)
{
    return max(max(A, B), max(C, D));
}
  
// Utility function to find
// the maximum scalar product
// from the equal length sub-sequences
// taken from the two given array
int maxProductUtil(
    int X, int Y,
    int* A, int* B,
    vector<vector<int> >& dp)
{
    // Return a very small number
    // if index is invalid
    if (X < 0 or Y < 0)
        return -INF;
  
    // If the sub-problem is already
    // evaluated, then return it
    if (dp[X][Y] != -1)
        return dp[X][Y];
  
    // Take the maximum of all
    // the recursive cases
    dp[X][Y]
        = maximum(
            A[X] * B[Y]
                + maxProductUtil(
                      X - 1, Y - 1, A, B, dp),
            A[X] * B[Y],
            maxProductUtil(
                X - 1, Y, A, B, dp),
            maxProductUtil(
                X, Y - 1, A, B, dp));
  
    return dp[X][Y];
}
  
// Function to find maximum scalar
// product from same size sub-sequences
// taken from the two given array
int maxProduct(int A[], int N,
               int B[], int M)
{
    // Initialize a 2-D array
    // for memoization
    vector<vector<int> >
    dp(N, vector<int>(M, -1));
  
    return maxProductUtil(
        N - 1, M - 1,
        A, B, dp);
}
  
// Driver Code
int main()
{
    int a[] = { -2, 6, -2, -5 };
    int b[] = { -3, 4, -2, 8 };
  
    int n = sizeof(a) / sizeof(a[0]);
    int m = sizeof(b) / sizeof(b[0]);
  
    cout << maxProduct(a, n, b, m);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to maximize
// product of same-indexed elements
// of same size subsequences
class GFG{
  
static final int INF = 10000000;
  
// Utility function to find the maximum
static int maximum(int A, int B, 
                   int C, int D)
{
    return Math.max(Math.max(A, B), 
                    Math.max(C, D));
}
  
// Utility function to find the 
// maximum scalar product from 
// the equal length sub-sequences
// taken from the two given array
static int maxProductUtil(int X, int Y,
                          int []A, int[] B,
                          int [][]dp)
{
      
    // Return a very small number
    // if index is invalid
    if (X < 0 || Y < 0)
        return -INF;
  
    // If the sub-problem is already
    // evaluated, then return it
    if (dp[X][Y] != -1)
        return dp[X][Y];
  
    // Take the maximum of all
    // the recursive cases
    dp[X][Y] = maximum(A[X] * B[Y] + 
                       maxProductUtil(X - 1, Y - 1,
                                      A, B, dp),
                       A[X] * B[Y], 
                       maxProductUtil(X - 1, Y, 
                                      A, B, dp),
                       maxProductUtil(X, Y - 1
                                      A, B, dp));
                                        
    return dp[X][Y];
}
  
// Function to find maximum scalar
// product from same size sub-sequences
// taken from the two given array
static int maxProduct(int A[], int N,
                      int B[], int M)
{
      
    // Initialize a 2-D array
    // for memoization
    int [][]dp = new int[N][M]; 
    for(int i = 0; i < N; i++)
    {
       for(int j = 0; j < M; j++)
       {
          dp[i][j] = -1;
       }
    }
      
    return maxProductUtil(N - 1, M - 1,
                          A, B, dp);
}
  
// Driver Code
public static void main(String[] args)
{
    int a[] = { -2, 6, -2, -5 };
    int b[] = { -3, 4, -2, 8 };
  
    int n = a.length;
    int m = b.length;
  
    System.out.print(maxProduct(a, n, b, m));
}
}
  
// This code is contributed by Amal Kumar Choubey

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to maximize
// product of same-indexed elements
// of same size subsequences
using System;
  
class GFG{
  
static readonly int INF = 10000000;
  
// Utility function to find the maximum
static int maximum(int A, int B, 
                   int C, int D)
{
    return Math.Max(Math.Max(A, B), 
                    Math.Max(C, D));
}
  
// Utility function to find the 
// maximum scalar product from 
// the equal length sub-sequences
// taken from the two given array
static int maxProductUtil(int X, int Y,
                          int []A, int[] B,
                          int [,]dp)
{
      
    // Return a very small number
    // if index is invalid
    if (X < 0 || Y < 0)
        return -INF;
  
    // If the sub-problem is already
    // evaluated, then return it
    if (dp[X, Y] != -1)
        return dp[X, Y];
  
    // Take the maximum of all
    // the recursive cases
    dp[X, Y] = maximum(A[X] * B[Y] + 
                       maxProductUtil(X - 1, Y - 1,
                                        A, B, dp),
                       A[X] * B[Y], 
                       maxProductUtil(X - 1, Y, 
                                      A, B, dp),
                       maxProductUtil(X, Y - 1, 
                                      A, B, dp));
                                          
    return dp[X, Y];
}
  
// Function to find maximum scalar
// product from same size sub-sequences
// taken from the two given array
static int maxProduct(int []A, int N,
                      int []B, int M)
{
      
    // Initialize a 2-D array
    // for memoization
    int [,]dp = new int[N, M]; 
    for(int i = 0; i < N; i++)
    {
       for(int j = 0; j < M; j++)
       {
          dp[i, j] = -1;
       }
    }
      
    return maxProductUtil(N - 1, M - 1,
                          A, B, dp);
}
  
// Driver Code
public static void Main(String[] args)
{
    int []a = { -2, 6, -2, -5 };
    int []b = { -3, 4, -2, 8 };
  
    int n = a.Length;
    int m = b.Length;
  
    Console.Write(maxProduct(a, n, b, m));
}
}
  
// This code is contributed by amal kumar choubey

chevron_right


Output:

54

competitive-programming-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Amal Kumar Choubey