-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_2614.java
34 lines (32 loc) · 1.09 KB
/
_2614.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.fishercoder.solutions.thirdthousand;
public class _2614 {
public static class Solution1 {
public int diagonalPrime(int[][] nums) {
int ans = 0;
boolean[] nonPrimes = generatePrimes((int) (Math.pow(10, 6) * 4));
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums[0].length; j++) {
if (i == j || i == nums.length - j - 1) {
if (!nonPrimes[nums[i][j]]) {
ans = Math.max(ans, nums[i][j]);
}
}
}
}
return ans;
}
private boolean[] generatePrimes(int n) {
boolean[] nonPrimes = new boolean[n];
// 1 is not a prime number
nonPrimes[1] = true;
for (int i = 2; i < n; i++) {
if (!nonPrimes[i]) {
for (int j = 2; i * j < n; j++) {
nonPrimes[i * j] = true;
}
}
}
return nonPrimes;
}
}
}