3115. Maximum Prime Difference LeetCode Solution
In this guide, you will get 3115. Maximum Prime Difference LeetCode Solution with the best time and space complexity. The solution to Maximum Prime Difference problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Maximum Prime Difference solution in C++
- Maximum Prime Difference solution in Java
- Maximum Prime Difference solution in Python
- Additional Resources

Problem Statement of 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.
Example 1:
Input: nums = [4,2,9,5,3]
Output: 3
Explanation: nums[1], nums[3], and nums[4] are prime. So the answer is |4 – 1| = 3.
Example 2:
Input: nums = [4,8,2,8]
Output: 0
Explanation: nums[2] is prime. Because there is just one prime number, the answer is |2 – 2| = 0.
Constraints:
1 <= nums.length <= 3 * 105
1 <= nums[i] <= 100
The input is generated such that the number of prime numbers in the nums is at least one.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(100) = O(1)
3115. Maximum Prime Difference LeetCode Solution in C++
class Solution {
public:
int maximumPrimeDifference(vector<int>& nums) {
constexpr int kMax = 100;
const vector<bool> isPrime = sieveEratosthenes(kMax + 1);
int minPrimeIndex = -1;
int maxPrimeIndex = -1;
for (int i = 0; i < nums.size(); ++i)
if (isPrime[nums[i]]) {
if (minPrimeIndex == -1)
minPrimeIndex = i;
maxPrimeIndex = i;
}
return maxPrimeIndex - minPrimeIndex;
}
private:
vector<bool> sieveEratosthenes(int n) {
vector<bool> isPrime(n, true);
isPrime[0] = false;
isPrime[1] = false;
for (int i = 2; i * i < n; ++i)
if (isPrime[i])
for (int j = i * i; j < n; j += i)
isPrime[j] = false;
return isPrime;
}
};
/* code provided by PROGIEZ */
3115. Maximum Prime Difference LeetCode Solution in Java
class Solution {
public int maximumPrimeDifference(int[] nums) {
final int kMax = 100;
boolean[] isPrime = sieveEratosthenes(kMax + 1);
int minPrimeIndex = -1;
int maxPrimeIndex = -1;
for (int i = 0; i < nums.length; ++i)
if (isPrime[nums[i]]) {
if (minPrimeIndex == -1)
minPrimeIndex = i;
maxPrimeIndex = i;
}
return maxPrimeIndex - minPrimeIndex;
}
private boolean[] sieveEratosthenes(int n) {
boolean[] isPrime = new boolean[n];
Arrays.fill(isPrime, true);
isPrime[0] = false;
isPrime[1] = false;
for (int i = 2; i * i < n; ++i)
if (isPrime[i])
for (int j = i * i; j < n; j += i)
isPrime[j] = false;
return isPrime;
}
};
// code provided by PROGIEZ
3115. Maximum Prime Difference LeetCode Solution in Python
class Solution:
def maximumPrimeDifference(self, nums: list[int]) -> int:
kMax = 100
isPrime = self._sieveEratosthenes(kMax + 1)
minPrimeIndex = -1
maxPrimeIndex = -1
for i, num in enumerate(nums):
if isPrime[num]:
if minPrimeIndex == -1:
minPrimeIndex = i
maxPrimeIndex = i
return maxPrimeIndex - minPrimeIndex
def _sieveEratosthenes(self, n: int) -> list[bool]:
isPrime = [True] * n
isPrime[0] = False
isPrime[1] = False
for i in range(2, int(n**0.5) + 1):
if isPrime[i]:
for j in range(i * i, n, i):
isPrime[j] = False
return isPrime
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.