Open In App

Maximize product of same-indexed elements of same size subsequences

Last Updated : 01 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 scalar 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 scalar 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++




// 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);
}


Java




// 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


Python3




# Python3 implementation to maximize
# product of same-indexed elements
# of same size subsequences
INF = 10000000
 
# Utility function to find the maximum
def maximum(A, B, C, 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
def maxProductUtil(X, Y, A, B, 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
def maxProduct(A, N, B, M):
 
    # Initialize a 2-D array
    # for memoization
    dp = [[-1 for i in range(m)]
              for i in range(n)]
 
    return maxProductUtil(N - 1, M - 1,
                          A, B, dp)
 
# Driver Code
a = [ -2, 6, -2, -5 ]
b = [ -3, 4, -2, 8 ]
n = len(a)
m = len(b)
 
print(maxProduct(a, n, b, m))
 
# This code is contributed by avanitrachhadiya2155


C#




// 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


Javascript




<script>
// Javascript implementation to maximize
// product of same-indexed elements
// of same size subsequences
 
let INF = 10000000;
  
// Utility function to find the maximum
function maximum(A, B, C, 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
function maxProductUtil(X, Y, A, B, 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
function maxProduct(A, N, B, M)
{
      
    // Initialize a 2-D array
    // for memoization
    let dp =new Array(N);
    for (var i = 0; i < dp.length; i++) {
    dp[i] = new Array(2);
    }
    for(let i = 0; i < N; i++)
    {
       for(let j = 0; j < M; j++)
       {
          dp[i][j] = -1;
       }
    }
      
    return maxProductUtil(N - 1, M - 1,
                          A, B, dp);
}
 
  // Driver Code
     
    let a = [ -2, 6, -2, -5 ];
    let b = [ -3, 4, -2, 8 ];
  
    let n = a.length;
    let m = b.length;
  
    document.write(maxProduct(a, n, b, m));
              
</script>


Output

54

Efficient approach: Using DP Tabulation method ( Iterative approach )

The approach to solve this problem is same but DP tabulation(bottom-up) method is better then Dp + memoization(top-down) because memoization method needs extra stack space of recursion calls.

Steps to solve this problem :

  • Create a DP to store the solution of the subproblems and initialize it with 0.
  • Initialize the DP with base cases dp[0][0] = A[0] * B[0].
  • Now Iterate over subproblems to get the value of current problem form previous computation of subproblems stored in DP\
  • Return the final solution stored in dp[N-1][M-1]

Implementation :

C++




// C++ program for above approach
 
#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 maxProduct(int A[], int N, int B[], int M)
{
      // Initialize dp to store
    // computations of subproblems
    vector<vector<int>> dp(N, vector<int>(M, 0));
     
    // Base case
    dp[0][0] = A[0] * B[0];
     
    // Fill first row
    for (int j = 1; j < M; j++) {
        dp[0][j] = max(A[0] * B[j], dp[0][j-1]);
    }
     
    // Fill first column
    for (int i = 1; i < N; i++) {
        dp[i][0] = max(A[i] * B[0], dp[i-1][0]);
    }
     
    // Fill remaining cells
    for (int i = 1; i < N; i++) {
        for (int j = 1; j < M; j++) {
            dp[i][j] = maximum(A[i] * B[j] + dp[i-1][j-1], A[i] * B[j], dp[i-1][j], dp[i][j-1]);
        }
    }
     
  // return final answer
    return dp[N-1][M-1];
}
 
// 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]);
     
      // function call
    cout << maxProduct(a, n, b, m);
}
 
// this code is contributed by bhardwajji


Java




// Java program for above approach
 
import java.util.*;
 
class Main
{
   
// 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 arrays
static int maxProduct(int[] A, int N, int[] B, int M)
{
   
    // Initialize dp to store computations of subproblems
    int[][] dp = new int[N][M];
 
    // Base case
    dp[0][0] = A[0] * B[0];
 
    // Fill first row
    for (int j = 1; j < M; j++) {
        dp[0][j] = Math.max(A[0] * B[j], dp[0][j - 1]);
    }
 
    // Fill first column
    for (int i = 1; i < N; i++) {
        dp[i][0] = Math.max(A[i] * B[0], dp[i - 1][0]);
    }
 
    // Fill remaining cells
    for (int i = 1; i < N; i++) {
        for (int j = 1; j < M; j++) {
            dp[i][j] = maximum(A[i] * B[j] + dp[i - 1][j - 1], A[i] * B[j], dp[i - 1][j], dp[i][j - 1]);
        }
    }
 
    // return final answer
    return dp[N - 1][M - 1];
}
 
// 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;
 
    // function call
    System.out.println(maxProduct(a, n, b, m));
}
}


Python3




def maximum(A, B, C, D):
    return max(max(A, B), max(C, D))
 
def maxProduct(A, N, B, M):
    # Initialize dp to store computations of subproblems
    dp = [[0] * M for _ in range(N)]
 
    # Base case
    dp[0][0] = A[0] * B[0]
 
    # Fill first row
    for j in range(1, M):
        dp[0][j] = max(A[0] * B[j], dp[0][j-1])
 
    # Fill first column
    for i in range(1, N):
        dp[i][0] = max(A[i] * B[0], dp[i-1][0])
 
    # Fill remaining cells
    for i in range(1, N):
        for j in range(1, M):
            dp[i][j] = maximum(A[i] * B[j] + dp[i-1][j-1], A[i] * B[j], dp[i-1][j], dp[i][j-1])
 
    # Return final answer
    return dp[N-1][M-1]
 
# Driver Code
a = [-2, 6, -2, -5]
b = [-3, 4, -2, 8]
 
n = len(a)
m = len(b)
 
# Function call
print(maxProduct(a, n, b, m))


C#




using System;
 
class MainClass {
    // 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 arrays
    static int MaxProduct(int[] A, int N, int[] B, int M)
    {
        // Initialize dp to store computations of
        // subproblems
        int[, ] dp = new int[N, M];
 
        // Base case
        dp[0, 0] = A[0] * B[0];
 
        // Fill first row
        for (int j = 1; j < M; j++) {
            dp[0, j] = Math.Max(A[0] * B[j], dp[0, j - 1]);
        }
 
        // Fill first column
        for (int i = 1; i < N; i++) {
            dp[i, 0] = Math.Max(A[i] * B[0], dp[i - 1, 0]);
        }
 
        // Fill remaining cells
        for (int i = 1; i < N; i++) {
            for (int j = 1; j < M; j++) {
                dp[i, j] = Maximum(
                    A[i] * B[j] + dp[i - 1, j - 1],
                    A[i] * B[j], dp[i - 1, j],
                    dp[i, j - 1]);
            }
        }
 
        // return final answer
        return dp[N - 1, M - 1];
    }
 
    // 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;
 
        // function call
        Console.WriteLine(MaxProduct(a, n, b, m));
    }
}
 
// The code is contributed by Samim Hossain Mondal.


Javascript




//Javascript code for this approach
 
// Utility function to find the maximum
 
function maximum(A, B, C, 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 arrays
function maxProduct(A, N, B, M) {
  // Initialize dp to store computations of subproblems
  let dp = new Array(N).fill().map(() => new Array(M).fill(0));
   
  // Base case
  dp[0][0] = A[0] * B[0];
   
  // Fill first row
  for (let j = 1; j < M; j++) {
    dp[0][j] = Math.max(A[0] * B[j], dp[0][j-1]);
  }
   
  // Fill first column
  for (let i = 1; i < N; i++) {
    dp[i][0] = Math.max(A[i] * B[0], dp[i-1][0]);
  }
   
  // Fill remaining cells
  for (let i = 1; i < N; i++) {
    for (let j = 1; j < M; j++) {
      dp[i][j] = maximum(A[i] * B[j] + dp[i-1][j-1], A[i] * B[j], dp[i-1][j], dp[i][j-1]);
    }
  }
   
  // return final answer
  return dp[N-1][M-1];
}
 
// Driver Code
let a = [ -2, 6, -2, -5 ];
let b = [ -3, 4, -2, 8 ];
 
let n = a.length;
let m = b.length;
 
// function call
console.log(maxProduct(a, n, b, m));
 
// This code is contributed by Sundaram


Output

54

Time complexity: O(N*M)
Auxiliary Space: O(N*M)



Similar Reads

Bitwise XOR of same indexed array elements after rearranging an array to make XOR of same indexed elements of two arrays equal
Given two arrays A[] and B[] consisting of N positive integers, the task is to the Bitwise XOR of same indexed array elements after rearranging the array B[] such that the Bitwise XOR of the same indexed elements of the arrays A[] becomes equal. Examples: Input: A[] = {1, 2, 3}, B[] = {4, 6, 7}Output: 5Explanation:Below are the possible arrangement
14 min read
Maximize sum of product of same-indexed elements of equal length subarrays obtained from two given arrays
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: 26Explanation:Subarrays selected fr
13 min read
Rearrange two given arrays to maximize sum of same indexed elements
Given two arrays A[] and B[] of size N, the task is to find the maximum possible sum of abs(A[i] – B[i]) by rearranging the array elements. Examples: Input: A[] = {1, 2, 3, 4, 5}, B[] = {1, 2, 3, 4, 5}Output: 12Explanation: One of the possible rearrangements of A[] is {5, 4, 3, 2, 1}. One of the possible rearrangements of B[] is {1, 2, 3, 4, 4}. Th
5 min read
Maximize minimum of array generated by maximums of same indexed elements of two rows of a given Matrix
Given a matrix mat[][] of N rows and M columns, the task is to choose any two rows(i, j) (0 ? i, j ? N - 1) and construct a new array A[] of size M, where A[k] = max(mat[i][k], mat[j][k]) such that minimum of A[] is maximum possible. Examples: Input: mat[][] = {{5, 0, 3, 1, 2}, {1, 8, 9, 1, 3}, {1, 2, 3, 4, 5}, {9, 1, 0, 3, 7}, {2, 3, 0, 6, 3}, {6,
7 min read
Maximize sum of Bitwise AND of same-indexed elements of a permutation of first N natural numbers and a given array
Given an array arr[] consisting of N positive integers, the task is to find the maximum sum of Bitwise AND of same-indexed elements of permutation of the first N natural numbers and the array arr[]. Examples: Input: arr[] = {4, 2, 3, 6}Output: 5Explanation: Consider the permutation {1, 0, 3, 2}. Sum of Bitwise AND of the above permutation and the g
9 min read
Rearrange an array to maximize sum of Bitwise AND of same-indexed elements with another array
Given two arrays A[] and B[] of sizes N, the task is to find the maximum sum of Bitwise AND of same-indexed elements in the arrays A[] and B[] that can be obtained by rearranging the array B[] in any order. Examples: Input: A[] = {1, 2, 3, 4}, B[] = {3, 4, 1, 2}Output: 10Explanation: One possible way is to obtain the maximum value is to rearrange t
15 min read
Count ways to construct array with even product from given array such that absolute difference of same indexed elements is at most 1
Given an array A[] of size N, the task is to count the numbers of ways to construct an array B[] of size N, such that the absolute difference at the same indexed elements must be less than or equal to 1, i.e. abs(A[i] - B[i]) ? 1, and the product of elements of the array B[] must be an even number. Examples: Input: A[] = { 2, 3 } Output: 7 Explanat
6 min read
Construct array with sum of product of same indexed elements in the given array equal to zero
Given an array, arr[] of size N (always even), the task is to construct a new array consisting of N non-zero integers such that the sum of the product of the same indexed elements of arr[] and the new array is equal to 0. If multiple solutions exist, print any one of them. Examples: Input: arr[] = {1, 2, 3, 4}Output: -4 -3 2 1 Explanation: Sum of p
6 min read
Minimize sum of product of same-indexed elements of two arrays by reversing a subarray of one of the two arrays
Given two equal-length arrays A[] and B[], consisting only of positive integers, the task is to reverse any subarray of the first array such that sum of the product of same-indexed elements of the two arrays, i.e. (A[i] * B[i]) is minimum. Examples: Input: N = 4, A[] = {2, 3, 1, 5}, B[] = {8, 2, 4, 3} Output: A[] = 1 3 2 5 B[] = 8 2 4 3 Minimum pro
12 min read
Maximize sum of odd-indexed array elements by repeatedly selecting at most 2*M array elements from the beginning
Given an array arr[] consisting of N integers and an integer M (initially 1), the task is to find the maximum sum of array elements chosen by Player A when two players A and B plays the game optimally according to the following rules: Player A starts the game.At every chance, X number of elements can be chosen from the beginning of the array, where
18 min read