All problems
0832MediumArrayMatrixPrefix Sum

Cross Product Panel

Tracked in this browser only
Write code

Trains the technique from

LeetCode 2906Construct Product Matrix

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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