Most Frequent Prime

You are given an m x n 0-indexed 2D matrix mat. From every cell, you can create numbers in the following way:

  • There could be at most 8 paths from the cells: east, south-east, south, south-west, west, north-west, north, and north-east.
  • Select a path from them and append digits in this path to the number being formed by traveling in this direction.
  • Numbers are generated at every step. For example, if the digits along the path are 1, 9, 1, then there will be three numbers generated along the way: 1, 19, and 191.

Return the most frequent prime number greater than 10 out of all the numbers created by traversing the matrix, or -1 if no such prime number exists. If there are multiple prime numbers with the highest frequency, return the largest among them.

Note: It is invalid to change the direction during the move.

Example 1
1 1
9 9
1 1
Inputmat = [[1,1],[9,9],[1,1]]
Output19
The most frequent prime number greater than 10 among all created numbers is 19.
Example 2
7
Inputmat = [[7]]
Output-1
The only number that can be formed is 7, which is prime but not greater than 10, so return -1.

Constraints

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

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate