Prime In Diagonal

You are given a 0-indexed two-dimensional integer array nums.

Return the largest prime number that lies on at least one of the diagonals of nums. In case no prime is present on any of the diagonals, return 0.

Note that:

  • An integer is prime if it is greater than 1 and has no positive integer divisors other than 1 and itself.
  • An integer val is on one of the diagonals of nums if there exists an integer i for which nums[i][i] = val or an i for which nums[i][nums.length - i - 1] = val.
Example 1
Inputnums = [[1,2,3],[5,6,7],[9,10,11]]
Output11
The numbers 1, 3, 6, 9, and 11 are the only numbers present on at least one of the diagonals, and 11 is the largest prime.
Example 2
Inputnums = [[1,2,3],[5,17,7],[9,11,10]]
Output17
The numbers 1, 3, 9, 10, and 17 are all present on at least one of the diagonals, and 17 is the largest prime.

Constraints

  • 1 <= nums.length <= 300
  • nums.length == numsi.length
  • 1 <= nums[i][j] <= 4*10^6

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