[LeetCode] Maximum Prime Difference

3115. Maximum Prime Difference

You are given an integer array nums.

Return an integer that is the maximum distance between the indices of two (not necessarily different) prime numbers in nums.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int sieve[111];
class Solution {
public:
int maximumPrimeDifference(vector<int>& A) {
for(int i = 0; i < 111; i++) sieve[i] = 0;
sieve[0] = sieve[1] = 1;
for(int i = 2; i < 111; i++) {
if(sieve[i]) continue;
for(int j = i * i; j < 111; j+=i) sieve[j] = 1;
}
int ma = -1, mi = A.size();
for(int i = 0; i < A.size(); i++) {
if(sieve[A[i]]) continue;
ma = max(ma, i);
mi = min(mi, i);
}
return ma - mi;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/04/14/PS/LeetCode/maximum-prime-difference/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.