# 311. Sparse Matrix Multiplication

Given two sparse matrices A and B, return the result of AB.

You may assume that A's column number is equal to B's row number.

Example:

A = \[ \[ 1, 0, 0], \[-1, 0, 3] ]

B = \[ \[ 7, 0, 0 ], \[ 0, 0, 0 ], \[ 0, 0, 1 ] ]

```
 |  1 0 0 |   | 7 0 0 |   |  7 0 0 |
```

AB = | -1 0 3 | x | 0 0 0 | = | -7 0 3 | | 0 0 1 |

## Solution <a href="#solution" id="solution"></a>

```java
public class Solution {
      public int[][] multiply(int[][] A, int[][] B) {
        int row1 = A.length;
        int col1 = A[0].length;
        int row2 = B.length;
        int col2 = B[0].length;

        int[][] result = new int[row1][col2];
        for (int i = 0; i < row1; i++) {
            for (int j = 0; j < col1; j++) {
                if (A[i][j] != 0) {

                    for (int k = 0; k < col2; k++) {
                        if (B[j][k] != 0) {
                            result[i][k] += A[i][j] * B[j][k];
                        }
                    }

                }
            }
        }

        return result;
    }
}
```


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://anand-aryan.gitbook.io/leetcode/311.-sparse-matrix-multiplication.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
