classSolution { staticintn=4 * (int) 1e6 + 1; staticboolean[] notPrime = newboolean[n]; static { notPrime[0] = true; notPrime[1] = true; for (inti=2; i < n; ++i) { if (!notPrime[i]) { if ((long) i * i < n) { for (intj= i * i; j < n; j+=i) { notPrime[j] = true; } } } } }
publicintdiagonalPrime(int[][] nums) { intres=0, m = nums.length, n = nums[0].length; for (inti=0; i < m; ++i) { if (!notPrime[nums[i][m - 1 - i]]) res = Math.max(res, nums[i][m - 1 - i]); if (!notPrime[nums[i][i]]) res = Math.max(res, nums[i][i]); } return res; }
classSolution { publiclong[] distance(int[] nums) { intn= nums.length; long[] res = newlong[n]; Map<Integer, List<Integer>> map = newHashMap<>(); for (inti=0; i < n; ++i) { intnum= nums[i]; if (!map.containsKey(num)) map.putIfAbsent(num, newArrayList<>()); map.get(num).add(i); } for (List<Integer> list : map.values()) { longfirst=0; for (int x : list) first += x - list.get(0);