Show problems as
Classic problem #2906MediumArrayMatrixPrefix Sum

Construct Product Matrix

Real-world version: Cross Product Panel
Tracked in this browser only
Read Construct Product Matrix on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A calibration panel is a grid of n rows by m columns of readings, given as grid.

Build a second grid panel of the same shape, where panel[i][j] is the product of every reading of grid except the one at row i, column j, taken modulo 12345.

Return panel.

Examples

Example 1

Input
grid = [[4, 7], [2, 5]]
Output
[[70, 40], [140, 56]]

For the cell holding 4 the other readings are 7, 2 and 5, whose product is 70. For the cell holding 7 they are 4, 2 and 5, giving 40. For 2 they are 4, 7 and 5, giving 140, and for 5 they are 4, 7 and 2, giving 56. All four are below 12345, so the remainder leaves them unchanged.

Example 2

Input
grid = [[6], [7]]
Output
[[7], [6]]

The panel has one column. The cell holding 6 has only the 7 left, and the cell holding 7 has only the 6.

Example 3

Input
grid = [[12345, 4]]
Output
[[4, 0]]

For the first cell the remaining product is 4. For the second it is 12345, and 12345 leaves a remainder of 0 on division by 12345.

Constraints

  • 1 <= grid.length <= 10^5
  • 1 <= grid[0].length <= 10^5
  • Every row of grid has the same length, and grid holds at least 2 readings in total
  • 1 <= grid[i][j] <= 10^9

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def cross_product_grid(grid: list[list[int]]) -> list[list[int]]:
Java
public int[][] crossProductGrid(int[][] grid)