Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 * 1051 <= nums[i] <= 100nums is at least one.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math
[4,2,9,5,3]
[4,8,2,8]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3115: Maximum Prime Difference
class Solution {
public int maximumPrimeDifference(int[] nums) {
for (int i = 0;; ++i) {
if (isPrime(nums[i])) {
for (int j = nums.length - 1;; --j) {
if (isPrime(nums[j])) {
return j - i;
}
}
}
}
}
private boolean isPrime(int x) {
if (x < 2) {
return false;
}
for (int v = 2; v * v <= x; ++v) {
if (x % v == 0) {
return false;
}
}
return true;
}
}
// Accepted solution for LeetCode #3115: Maximum Prime Difference
func maximumPrimeDifference(nums []int) int {
for i := 0; ; i++ {
if isPrime(nums[i]) {
for j := len(nums) - 1; ; j-- {
if isPrime(nums[j]) {
return j - i
}
}
}
}
}
func isPrime(n int) bool {
if n < 2 {
return false
}
for i := 2; i <= n/i; i++ {
if n%i == 0 {
return false
}
}
return true
}
# Accepted solution for LeetCode #3115: Maximum Prime Difference
class Solution:
def maximumPrimeDifference(self, nums: List[int]) -> int:
def is_prime(x: int) -> bool:
if x < 2:
return False
return all(x % i for i in range(2, int(sqrt(x)) + 1))
for i, x in enumerate(nums):
if is_prime(x):
for j in range(len(nums) - 1, i - 1, -1):
if is_prime(nums[j]):
return j - i
// Accepted solution for LeetCode #3115: Maximum Prime Difference
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3115: Maximum Prime Difference
// class Solution {
// public int maximumPrimeDifference(int[] nums) {
// for (int i = 0;; ++i) {
// if (isPrime(nums[i])) {
// for (int j = nums.length - 1;; --j) {
// if (isPrime(nums[j])) {
// return j - i;
// }
// }
// }
// }
// }
//
// private boolean isPrime(int x) {
// if (x < 2) {
// return false;
// }
// for (int v = 2; v * v <= x; ++v) {
// if (x % v == 0) {
// return false;
// }
// }
// return true;
// }
// }
// Accepted solution for LeetCode #3115: Maximum Prime Difference
function maximumPrimeDifference(nums: number[]): number {
const isPrime = (x: number): boolean => {
if (x < 2) {
return false;
}
for (let i = 2; i <= x / i; i++) {
if (x % i === 0) {
return false;
}
}
return true;
};
for (let i = 0; ; ++i) {
if (isPrime(nums[i])) {
for (let j = nums.length - 1; ; --j) {
if (isPrime(nums[j])) {
return j - i;
}
}
}
}
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.