FastPrepMaximum Trailing Zeros on a Path with at Most One Turn

Maximum Trailing Zeros on a Path with at Most One Turn

Marshall Wace logoMarshall Wace● MediumNEW GRADOA

Problem statement

You are given a rectangular matrix A of positive integers. A path consists of cells that share a side with the next cell.

The path may start and end at any cells. It moves horizontally or vertically and may change direction by a left or right turn at most once. A single cell and a straight path are allowed.

Multiply the values in the path. Return the greatest possible number of trailing zeros in this product over all valid paths.

Function

solution(A: int[][]) → int

Examples

Example 1

A = [[10,20],[5,2]]return = 3

The path through values 5, 10, 20 turns once and has product 1000, with 3 trailing zeros. No allowed path has more.

Example 2

A = [[2,4],[8,16]]return = 0

Every value is a power of 2. No path product contains a factor of 5, so none ends in a zero.

Example 3

A = [[10]]return = 1

The only path is the single cell, whose value 10 has one trailing zero.

Constraints

  • 1 <= A.length <= 1000 and 1 <= A[0].length <= 1000.
  • The total number of cells is at most 200000.
  • All rows have the same length.
  • 1 <= A[i][j] <= 10^9.

More Marshall Wace problems

See Marshall Wace hiring insights
Saved
public int solution(int[][] A) {
    // Write your solution here.
}
A[[10,20],[5,2]]
expected3
Checking account…