2906. Construct Product Matrix LeetCode Solution

In this guide, you will get 2906. Construct Product Matrix LeetCode Solution with the best time and space complexity. The solution to Construct Product Matrix problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Construct Product Matrix solution in C++
  4. Construct Product Matrix solution in Java
  5. Construct Product Matrix solution in Python
  6. Additional Resources
2906. Construct Product Matrix LeetCode Solution image

Problem Statement of Construct Product Matrix

Given a 0-indexed 2D integer matrix grid of size n * m, we define a 0-indexed 2D matrix p of size n * m as the product matrix of grid if the following condition is met:

Each element p[i][j] is calculated as the product of all elements in grid except for the element grid[i][j]. This product is then taken modulo 12345.

Return the product matrix of grid.

Example 1:

Input: grid = [[1,2],[3,4]]
Output: [[24,12],[8,6]]
Explanation: p[0][0] = grid[0][1] * grid[1][0] * grid[1][1] = 2 * 3 * 4 = 24
p[0][1] = grid[0][0] * grid[1][0] * grid[1][1] = 1 * 3 * 4 = 12
p[1][0] = grid[0][0] * grid[0][1] * grid[1][1] = 1 * 2 * 4 = 8
p[1][1] = grid[0][0] * grid[0][1] * grid[1][0] = 1 * 2 * 3 = 6
So the answer is [[24,12],[8,6]].
Example 2:

Input: grid = [[12345],[2],[1]]
Output: [[2],[0],[0]]
Explanation: p[0][0] = grid[0][1] * grid[0][2] = 2 * 1 = 2.
p[0][1] = grid[0][0] * grid[0][2] = 12345 * 1 = 12345. 12345 % 12345 = 0. So p[0][1] = 0.
p[0][2] = grid[0][0] * grid[0][1] = 12345 * 2 = 24690. 24690 % 12345 = 0. So p[0][2] = 0.
So the answer is [[2],[0],[0]].

Constraints:

1 <= n == grid.length <= 105
1 <= m == grid[i].length <= 105
2 <= n * m <= 105
1 <= grid[i][j] <= 109

Complexity Analysis

  • Time Complexity: O(mn)
  • Space Complexity: O(mn)
See also  1752. Check if Array Is Sorted and Rotated LeetCode Solution

2906. Construct Product Matrix LeetCode Solution in C++

class Solution {
 public:
  vector<vector<int>> constructProductMatrix(vector<vector<int>>& grid) {
    constexpr int kMod = 12345;
    const int m = grid.size();
    const int n = grid[0].size();
    vector<vector<int>> ans(m, vector<int>(n));
    vector<int> prefix{1};
    int suffix = 1;

    for (const vector<int>& row : grid)
      for (const int num : row)
        prefix.push_back(static_cast<long>(prefix.back()) * num % kMod);

    for (int i = m - 1; i >= 0; --i)
      for (int j = n - 1; j >= 0; --j) {
        ans[i][j] = prefix[i * n + j] * suffix % kMod;
        suffix = static_cast<long>(suffix) * grid[i][j] % kMod;
      }

    return ans;
  }
};
/* code provided by PROGIEZ */

2906. Construct Product Matrix LeetCode Solution in Java

class Solution {
  public int[][] constructProductMatrix(int[][] grid) {
    final int kMod = 12345;
    final int m = grid.length;
    final int n = grid[0].length;
    int[][] ans = new int[m][n];
    List<Integer> prefix = new ArrayList<>(List.of(1));
    int suffix = 1;

    for (int[] row : grid)
      for (int num : row)
        prefix.add((int) ((long) prefix.get(prefix.size() - 1) * num % kMod));

    for (int i = m - 1; i >= 0; i--)
      for (int j = n - 1; j >= 0; j--) {
        ans[i][j] = (int) ((long) prefix.get(i * n + j) * suffix % kMod);
        suffix = (int) ((long) suffix * grid[i][j] % kMod);
      }

    return ans;
  }
}
// code provided by PROGIEZ

2906. Construct Product Matrix LeetCode Solution in Python

class Solution:
  def constructProductMatrix(self, grid: list[list[int]]) -> list[list[int]]:
    kMod = 12345
    m = len(grid)
    n = len(grid[0])
    ans = [[0] * n for _ in range(m)]
    prefix = [1]
    suffix = 1

    for row in grid:
      for num in row:
        prefix.append(prefix[-1] * num % kMod)

    for i in reversed(range(m)):
      for j in reversed(range(n)):
        ans[i][j] = prefix[i * n + j] * suffix % kMod
        suffix = suffix * grid[i][j] % kMod

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.