> For the complete documentation index, see [llms.txt](https://gl01.gitbook.io/gfg-editorials/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gl01.gitbook.io/gfg-editorials/2024/04-2024-april-4/07-maximize-dot-product.md).

# 07. Maximize dot product

The problem can be found at the following link: [Question Link](https://www.geeksforgeeks.org/problems/maximize-dot-product2649/1)

## My Approach

To solve this problem, we use dynamic programming. We initialize a 2D array `dp` of size `(n+1) x (m+1)` to store the maximum dot product for subarrays of `a` and `b`. - We initialize `dp[0][j]` to `INT_MIN` for handling cases where one of the arrays is empty.

* Then, we iterate over the arrays `a` and `b`, and for each pair of elements, we update `dp[i][j]` to be the maximum of either the dot product of the subarrays ending at index `i` and `j` or the dot product of the subarrays ending at index `i-1` and `j`.
* Finally, we return `dp[n][m]` which represents the maximum dot product.

## Time and Auxiliary Space Complexity

* **Time Complexity** : `O(n * m)`, where `n` is the size of array `a` and `m` is the size of array `b`.
* **Auxiliary Space Complexity** : `O(n * m)`, for the `dp` array.

## Code (C++)

```cpp
class Solution {
public:
    int maxDotProduct(int n, int m, int a[], int b[]) 
    { 
        vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));
        
        for (int j = 1; j <= m; ++j)
            dp[0][j] = INT_MIN;
        
        for (int i = 1; i <= n; ++i)
            for (int j = 1; j <= m; ++j)
                dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1] + a[i - 1] * b[j - 1]);
        
        return dp[n][m];
    } 
};
```

## Contribution and Support

For discussions, questions, or doubts related to this solution, please visit our [discussion section](https://github.com/getlost01/gfg-potd/discussions). We welcome your input and aim to foster a collaborative learning environment.

If you find this solution helpful, consider supporting us by giving a ⭐ star to the [getlost01/gfg-potd](https://github.com/getlost01/gfg-potd) repository.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://gl01.gitbook.io/gfg-editorials/2024/04-2024-april-4/07-maximize-dot-product.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
