2614. Prime In Diagonal
```java
class Solution {
public boolean[] getPrimes(int n) {
boolean[] primes = new boolean[n];
Arrays.fill(primes, true);
primes[0] = false;
primes[1] = false;
for (int i = 2; i*i < n; i++) {
if (primes[i]) {
for (int j = i*i; j < n; j+=i) {
primes[j] = false;
}
}
}
return primes;
}
public int diagonalPrime(int[][] nums) {
boolean[] isPrime = getPrimes(4000001);
int ans = 0;
for (int i = 0; i < nums.length; i++) {
int a = nums[i][i];
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;
}
}
```
PreviousWeekly Contest 340Next2615. Sum of Distances (same as 2121. Intervals Between Identical Elements)
Last updated