Maximum Prime Difference - Problem
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.
The distance between two indices i and j is |i - j|.
A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself.
Input & Output
Example 1 — Multiple Primes
$
Input:
nums = [4,2,9,5,3]
›
Output:
3
💡 Note:
Prime numbers are 2 (index 1), 5 (index 3), and 3 (index 4). Maximum distance is between indices 1 and 4: |4-1| = 3.
Example 2 — Same Prime Twice
$
Input:
nums = [4,8,2,8]
›
Output:
0
💡 Note:
Only one prime number 2 at index 2. Distance between the same index is 0.
Example 3 — Edge Positions
$
Input:
nums = [2,4,6,8,11]
›
Output:
4
💡 Note:
Primes are 2 (index 0) and 11 (index 4). Maximum distance is |4-0| = 4.
Constraints
- 1 ≤ nums.length ≤ 3 × 105
- 1 ≤ nums[i] ≤ 106
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code