Ctrl K

Zeros Along an L-Shaped Path

Medium
You get a rectangular table board of positive integers. Consider paths through side-adjacent cells that first go straight, horizontally or vertically, and then may turn by 90° once and go straight again. No cell is visited twice. A path may also have no turn at all, or even consist of a single cell. Multiply the numbers in all cells of the path. Return the largest number of trailing zeros such a product can have.
Example 1
board=[[4, 7, 25], [3, 5, 2], [8, 1, 6]] → 2

The path 4 → 7 → 25, then down through 2 → 6, has product 4·7·25·2·6 = 8400, which ends in two zeros. No path does better.

Example 2
board=[[3, 7], [9, 11]] → 0

No cell has a factor of 2 or 5, so no product ends in a zero.

Example 3
board=[[10, 2, 5, 50]] → 3
Constraints
  • The table is m × n with 1 ≤ m, n
  • m · n ≤ 10⁵
  • 1 ≤ board[i][j] ≤ 1000
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLargest Light Square MediumNeighbourhood Sums in a Grid MediumK-th Largest Corner XOR MediumThree Heaviest Diamonds MediumBalanced Square in a Grid Medium
loading Python…
Input
board=[[4, 7, 25], [3, 5, 2], [8, 1, 6]]
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 79 tests on the server, hidden ones included.