Skip to content

Practice Most Frequent Prime

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#3044
LeetCode Problem

Most Frequent Prime

Problem Statement

Given the digit matrix mat, start at every cell and follow each possible straight direction: north, northeast, east, southeast, south, southwest, west, or northwest. Append each visited digit to the number being formed, and count a number at every step along the path. A path cannot turn or leave the matrix. Among the generated prime numbers greater than 10, return the most frequent one; break frequency ties by choosing the larger prime. Return -1 if none exists.

Source: Most Frequent Prime on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
mat = [[1,1],[9,9],[1,1]]
Output
19

# Example 2

Input
mat = [[7]]
Output
-1

# Example 3

Input
mat = [[9,7,8],[4,6,5],[2,8,6]]
Output
97

Constraints

m == mat.length
n == mat[i].length
1 <= m, n <= 6
1 <= mat[i][j] <= 9

Related LeetCode Problems

Practice more problems similar to Most Frequent Prime.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.