Given two arrays of positive integers of size m and n where m > n. We need to maximize the dot product by inserting zeros in the second array but we cannot disturb the order of elements.

Examples:

Input : A[] = {2, 3 , 1, 7, 8} B[] = {3, 6, 7} Output : 107 Explanation : We get maximum dot product after inserting 0 at first and third positions in second array. Maximum Dot Product : = A[i] * B[j] 2*0 + 3*3 + 1*0 + 7*6 + 8*7 = 107 Input : A[] = {1, 2, 3, 6, 1, 4} B[] = {4, 5, 1} Output : 46

Asked in: Directi Interview

Another way to look at this problem is, for every pair of elements element A[i] and B[j] where j >= i , we have two choices:

- We multiply A[i] and B[j] and add to product (We include A[i]).
- We exclude A[i] from product (In other words, we insert 0 at current position in B[])

The idea is to use Dynamic programing .

1) Given Array A[] of size 'm' and B[] of size 'n' 2) Create 2D matrix 'DP[n + 1][m + 1]' initialize it with '0' 3) Run loop outer loop for i = 1 to n Inner loop j = i to m // Two cases arise // 1) Include A[j] // 2) Exclude A[j] (insert 0 in B[]) dp[i][j] = max(dp[i-1][j-1] + A[j-1] * B[i -1], dp[i][j-1]) // Last return maximum dot product that is return dp[n][m]

Below is the implementation of above idea.

## C++

`// C++ program to find maximum dot product of two array ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function compute Maximum Dot Product and ` `// return it ` `long` `long` `int` `MaxDotProduct(` `int` `A[], ` `int` `B[], ` ` ` `int` `m, ` `int` `n) ` `{ ` ` ` `// Create 2D Matrix that stores dot product ` ` ` `// dp[i+1][j+1] stores product considering B[0..i] ` ` ` `// and A[0...j]. Note that since all m > n, we fill ` ` ` `// values in upper diagonal of dp[][] ` ` ` `long` `long` `int` `dp[n+1][m+1]; ` ` ` `memset` `(dp, 0, ` `sizeof` `(dp)); ` ` ` ` ` `// Traverse through all elements of B[] ` ` ` `for` `(` `int` `i=1; i<=n; i++) ` ` ` ` ` `// Consider all values of A[] with indexes greater ` ` ` `// than or equal to i and compute dp[i][j] ` ` ` `for` `(` `int` `j=i; j<=m; j++) ` ` ` ` ` `// Two cases arise ` ` ` `// 1) Include A[j] ` ` ` `// 2) Exclude A[j] (insert 0 in B[]) ` ` ` `dp[i][j] = max((dp[i-1][j-1] + (A[j-1]*B[i-1])) , ` ` ` `dp[i][j-1]); ` ` ` ` ` `// return Maximum Dot Product ` ` ` `return` `dp[n][m] ; ` `} ` ` ` `// Driver program to test above function ` `int` `main() ` `{ ` ` ` `int` `A[] = { 2, 3 , 1, 7, 8 } ; ` ` ` `int` `B[] = { 3, 6, 7 } ; ` ` ` `int` `m = ` `sizeof` `(A)/` `sizeof` `(A[0]); ` ` ` `int` `n = ` `sizeof` `(B)/` `sizeof` `(B[0]); ` ` ` `cout << MaxDotProduct(A, B, m, n); ` ` ` `return` `0; ` `} ` |

## Java

`// Java program to find maximum ` `// dot product of two array ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` `// Function to compute Maximum ` `// Dot Product and return it ` `static` `int` `MaxDotProduct(` `int` `A[], ` `int` `B[], ` `int` `m, ` `int` `n) ` `{ ` ` ` `// Create 2D Matrix that stores dot product ` ` ` `// dp[i+1][j+1] stores product considering B[0..i] ` ` ` `// and A[0...j]. Note that since all m > n, we fill ` ` ` `// values in upper diagonal of dp[][] ` ` ` `int` `dp[][] = ` `new` `int` `[n + ` `1` `][m + ` `1` `]; ` ` ` `for` `(` `int` `[] row : dp) ` ` ` `Arrays.fill(row, ` `0` `); ` ` ` ` ` `// Traverse through all elements of B[] ` ` ` `for` `(` `int` `i = ` `1` `; i <= n; i++) ` ` ` ` ` `// Consider all values of A[] with indexes greater ` ` ` `// than or equal to i and compute dp[i][j] ` ` ` `for` `(` `int` `j = i; j <= m; j++) ` ` ` ` ` `// Two cases arise ` ` ` `// 1) Include A[j] ` ` ` `// 2) Exclude A[j] (insert 0 in B[]) ` ` ` `dp[i][j] = ` ` ` `Math.max((dp[i - ` `1` `][j - ` `1` `] + ` ` ` `(A[j - ` `1` `] * B[i - ` `1` `])), dp[i][j - ` `1` `]); ` ` ` ` ` `// return Maximum Dot Product ` ` ` `return` `dp[n][m]; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) { ` ` ` `int` `A[] = {` `2` `, ` `3` `, ` `1` `, ` `7` `, ` `8` `}; ` ` ` `int` `B[] = {` `3` `, ` `6` `, ` `7` `}; ` ` ` `int` `m = A.length; ` ` ` `int` `n = B.length; ` ` ` `System.out.print(MaxDotProduct(A, B, m, n)); ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

Output:

107

Time Complexity : O(nm)

This article is contributed by **Nishant Singh**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Choose maximum weight with given weight and value ratio
- Largest sum Zigzag sequence in a matrix
- Finding the maximum square sub-matrix with all equal elements
- Maximum points collected by two persons allowed to meet once
- Find element using minimum segments in Seven Segment Display
- Probability of Knight to remain in the chessboard
- LCS (Longest Common Subsequence) of three strings
- Sort an array when two halves are sorted
- Unbounded Knapsack (Repetition of items allowed)
- Maximize number of 0s by flipping a subarray
- Maximum Product Cutting | DP-36
- Egg Dropping Puzzle | DP-11
- 0-1 Knapsack Problem | DP-10
- Min Cost Path | DP-6
- Optimal Substructure Property in Dynamic Programming | DP-2