Construct Product Matrix

Given a 0-indexed 2D integer matrix grid of size n * m, 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
1 2
3 4
Inputgrid = [[1,2],[3,4]]
Output[[24,12],[8,6]]
Each cell contains the product of all other elements in grid, producing [[24,12],[8,6]].
Example 2
12345
    2
    1
Inputgrid = [[12345],[2],[1]]
Output[[2],[0],[0]]
The products excluding each element are 2, 12345, and 24690, which modulo 12345 become 2, 0, and 0.

Constraints

  • 1 <= n == grid.length <= 10^5
  • 1 <= m == grid[i].length <= 10^5
  • 2 <= n * m <= 10^5
  • 1 <= grid[i][j] <= 10^9

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate