- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find Maximum dot product of two arrays with insertion of 0's in C++

Suppose we have two arrays of positive integers of size m and n. The m > n. We have to maximize the dot product by inserting zeros in the second array. One thing we have to keep in mind that we will not change the ordering of the elements in the given arrays. Suppose the arrays are A = [2, 3, 1, 7, 8], and another array B = [3, 6, 7]. The output will be 107. We can maximize the dot product after inserting 0s at first and third position of the second array. So the product will be 2 * 0 + 3 * 3 + 1 * 0 + 7 * 6 + 8 * 7 = 107.

To solve this, we will use the Dynamic programming approach. So the size of A is m, and size of B is n. We will create one table for dynamic programming of the order (n + 1)x(m + 1), and fill it with 0s. Now use these steps to do the rest −

For i in range 1 to n:

For j := i to m

For j := i to m

table[i, j] := max(table[i - 1, j - 1] + A[j - 1] * B[i - 1], table[i, j - 1])

## Example

#include <iostream> using namespace std; long long int findMaximumDotProd(int A[], int B[], int m, int n) { long long int table[n+1][m+1]; for(int i = 0; i<=n; i++){ for(int j = 0; j<=m; j++){ table[i][j] = 0; } } for (int i=1; i<=n; i++) for (int j=i; j<=m; j++) table[i][j] = max((table[i-1][j-1] + (A[j-1]*B[i-1])) , table[i][j-1]); return table[n][m] ; } 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 << "Maximum dot product: " << findMaximumDotProd(A, B, m, n); }

### Output

Maximum dot product: 107

- Related Questions & Answers
- Return the dot product of two masked arrays in Numpy
- How to find the dot product of two matrices in R?
- How to find the dot product of two pandas series objects?
- Return the dot product of two vectors in Python
- C++ Program for dot product and cross product of two vectors
- Program to find out the dot product of two sparse vectors in Python
- Matrix product of two arrays in Numpy
- Return the dot product of two multidimensional vectors in Python
- Find Sum of pair from two arrays with maximum sum in C++
- Return the dot product of two masked arrays and set whether masked data is propagated in Numpy
- Compute the tensor dot product for arrays with different dimensions in Python
- Maximum Sum of Products of Two Arrays in C++
- Get the Inner product of two arrays in Python
- Get the Kronecker product of two arrays in Python
- Get the Outer product of two arrays in Python