2614. Prime In Diagonal LeetCode Solution

In this guide, you will get 2614. Prime In Diagonal LeetCode Solution with the best time and space complexity. The solution to Prime In Diagonal problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Prime In Diagonal solution in C++
  4. Prime In Diagonal solution in Java
  5. Prime In Diagonal solution in Python
  6. Additional Resources
2614. Prime In Diagonal LeetCode Solution image

Problem Statement of 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.

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 <= 300
nums.length == numsi.length
1 <= nums[i][j] <= 4*106

Complexity Analysis

  • Time Complexity: O(n \cdot \sqrt{\max(\texttt{nums})})
  • Space Complexity: O(1)

2614. Prime In Diagonal LeetCode Solution in C++

class Solution {
 public:
  int diagonalPrime(vector<vector<int>>& nums) {
    int ans = 0;
    for (int i = 0; i < nums.size(); ++i) {
      const int a = nums[i][i];
      const int b = nums[i][nums.size() - i - 1];
      if (isPrime(a))
        ans = max(ans, a);
      if (isPrime(b))
        ans = max(ans, b);
    }
    return ans;
  }

 private:
  bool isPrime(int n) {
    if (n <= 1)
      return false;
    for (int i = 2; i * i <= n; ++i)
      if (n % i == 0)
        return false;
    return true;
  }
};
/* code provided by PROGIEZ */

2614. Prime In Diagonal LeetCode Solution in Java

class Solution {
  public int diagonalPrime(int[][] nums) {
    int ans = 0;
    for (int i = 0; i < nums.length; ++i) {
      final int a = nums[i][i];
      final int b = nums[i][nums.length - i - 1];
      if (isPrime(a))
        ans = Math.max(ans, a);
      if (isPrime(b))
        ans = Math.max(ans, b);
    }
    return ans;
  }

  private boolean isPrime(int n) {
    if (n <= 1)
      return false;
    for (int i = 2; i * i <= n; ++i)
      if (n % i == 0)
        return false;
    return true;
  }
}
// code provided by PROGIEZ

2614. Prime In Diagonal LeetCode Solution in Python

class Solution:
  def diagonalPrime(self, nums: list[list[int]]) -> int:
    def isPrime(n: int) -> bool:
      if n <= 1:
        return False
      for i in range(2, int(n**0.5) + 1):
        if n % i == 0:
          return False
      return True

    primes1 = [row[i]
               for i, row in enumerate(nums) if isPrime(row[i])]
    primes2 = [row[-1 - i]
               for i, row in enumerate(nums) if isPrime(row[-1 - i])]
    return max(max(primes1) if primes1 else 0,
               max(primes2) if primes2 else 0)
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.