1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| class Solution { public int maximumPrimeDifference(int[] nums) { int i = 0, j = nums.length - 1; while (!isPrime(nums[i])) { i++; } while (!isPrime(nums[j])) { j--; } return j - i; }
private boolean isPrime(int num) { for (int i = 2; i * i <= num; i++) { if (num % i == 0) { return false; } } return num != 1; } }
|
Reference
3115. Maximum Prime Difference