Maximum Trailing Zeros in a Cornered Path - Problem

You are given a 2D integer array grid of size m × n, where each cell contains a positive integer.

A cornered path is a special type of path that can change direction at most once. The path moves exclusively in one direction (horizontal or vertical) until it reaches a turn point, then switches to the perpendicular direction for the remainder of the journey. No cell can be visited twice!

Examples of valid cornered paths:

  • Pure horizontal: → → → →
  • Pure vertical: ↓ ↓ ↓
  • L-shaped: → → → ↓ ↓
  • Reverse L: ↑ ↑ ← ← ←

Your task is to find the cornered path whose product of all values has the maximum number of trailing zeros. Trailing zeros come from factors of 10, which means we need to count factors of 2 and 5 in the product!

Input & Output

Constraints

Asked in
25.0K Views
Medium Frequency
~15 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen