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
8paths 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, and191.
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
Input
mat = [[1,1],[9,9],[1,1]]Output
19The most frequent prime number greater than 10 among all created numbers is 19.
Example 2
7
Input
mat = [[7]]Output
-1The 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