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:
1 and has no positive integer divisors other than 1 and itself.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.
In the above diagram, one diagonal is [1,5,9] and another diagonal is [3,5,7].
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.
Constraints:
1 <= nums.length <= 300nums.length == numsi.length1 <= nums[i][j] <= 4*106When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to this problem involves checking every number along the diagonals of a grid to see if it's a prime number. We will examine each relevant number one by one, testing its primality.
Here's how the algorithm would work step-by-step:
def prime_in_diagonal_brute_force(matrix):
largest_prime = 0
matrix_size = len(matrix)
# Check main diagonal
for index in range(matrix_size):
number_to_check = matrix[index][index]
if number_to_check > 1:
is_prime = True
# Primality test
for divisor in range(2, number_to_check):
if number_to_check % divisor == 0:
is_prime = False
break
# Update largest prime if needed
if is_prime:
if number_to_check > largest_prime:
largest_prime = number_to_check
# Check other diagonal
for index in range(matrix_size):
number_to_check = matrix[index][matrix_size - 1 - index]
# We want to only check numbers > 1
if number_to_check > 1:
is_prime = True
# Primality test
for divisor in range(2, number_to_check):
if number_to_check % divisor == 0:
is_prime = False
break
# Update largest prime if needed
if is_prime:
# Update the largest prime
if number_to_check > largest_prime:
largest_prime = number_to_check
return largest_primeTo find prime numbers within the diagonals of a grid, we only need to check the numbers present on those diagonals. We'll focus on extracting these diagonal numbers and efficiently testing each one for primality.
Here's how the algorithm would work step-by-step:
def prime_in_diagonal(matrix):
maximum_prime = 0
number_of_rows = len(matrix)
for i in range(number_of_rows):
# Check numbers on the main diagonal
main_diagonal_element = matrix[i][i]
if main_diagonal_element > 1:
if is_prime(main_diagonal_element):
maximum_prime = max(maximum_prime, main_diagonal_element)
# Check numbers on the anti-diagonal
anti_diagonal_element = matrix[i][number_of_rows - 1 - i]
# Prevent double-counting the center element
if anti_diagonal_element > 1 and i != number_of_rows - 1 - i:
if is_prime(anti_diagonal_element):
maximum_prime = max(maximum_prime, anti_diagonal_element)
return maximum_prime
def is_prime(number):
# Numbers less than 2 cannot be prime
if number < 2:
return False
# Only need to check divisibility up to the square root
for i in range(2, int(number**0.5) + 1):
if number % i == 0:
return False
return True| Case | How to Handle |
|---|---|
| Null or empty matrix | Return 0 immediately since no diagonal elements exist. |
| Non-square matrix | Only process the minimum of rows and columns to avoid index out of bounds exceptions. |
| Matrix with only one element | Check if the single element is prime and return it or 0 if not prime. |
| Matrix with negative numbers | Prime numbers are greater than 1, so ignore any negative numbers encountered. |
| Matrix containing zero | Zero is not a prime number, so it should be ignored. |
| Large numbers in the matrix (potential overflow) | Use appropriate data types (e.g., long) to prevent integer overflow when checking for primality. |
| All numbers in the matrix are non-prime | Return 0, as no prime numbers were found on the diagonals. |
| Numbers on both diagonals are the same, and prime. | Return the duplicate prime number without error. |