#2614

Prime In Diagonal

pupil · 500 · lc easy +27 · verified · 37.4% accepted · 406 likes · top 16%

Description

You are given a 0-indexed 2D integer array nums. The two main diagonals consist of cells where nums[i][i] or nums[i][nums.length - i - 1] for some i. Return the largest prime number on either diagonal, or 0 if none exists.

Example 1:

Input: nums = [[1,2,3],[5,6,7],[9,10,11]]
Output: 11
Explanation: The numbers 1, 3, 6, 9, and 11 are the only numbers present on at least one of the diagonals. Since 11 is the largest prime, we return 11.

Example 2:

Input: nums = [[1,2,3],[5,17,7],[9,11,10]]
Output: 17
Explanation: The numbers 1, 3, 9, 10, and 17 are all present on at least one of the diagonals. 17 is the largest prime, so we return 17.

Code

1
2
3