Skip to main content

3115. Maximum Prime Difference

Problem Description​

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.

Examples​

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 * 10^5
  • 1 <= nums[i] <= 100
  • The input is generated such that the number of prime numbers in the nums is at least one.

Solution for3115. Maximum Prime Difference​

1. Sieve of Eratosthenes​

  • Use the Sieve of Eratosthenes to generate a list of prime numbers up to the maximum number in the array.

2. Find Prime Indices​

  • Iterate through the array and record the indices of all prime numbers.

3. Calculate Maximum Difference​

  • Sort the list of indices and find the difference between the maximum and minimum indices.

Detailed Steps in Code​

  1. Sieve of Eratosthenes:

    • Initialize a boolean array prime where prime[i] indicates whether i is a prime number.
    • Set all values in prime to true, except for prime[0] and prime[1] which are set to false.
    • For each number p from 2 to the square root of n, mark all multiples of p as false.
  2. Find Prime Indices:

    • Iterate through the array nums and store the indices of prime numbers in a list p.
  3. Calculate Maximum Difference:

    • Sort the list p of indices.
    • The maximum difference is the difference between the last and first elements in the sorted list p.

Implementation​

Live Editor
function Solution(arr) {
    function SieveOfEratosthenes(n) {
    let prime = Array(n + 1).fill(true);
    prime[0] = prime[1] = false;

    for (let p = 2; p * p <= n; p++) {
        if (prime[p]) {
            for (let i = p * p; i <= n; i += p) {
                prime[i] = false;
            }
        }
    }

    return prime;
}

function maximumPrimeDifference(nums) {
    let maxi = Math.max(...nums);
    let prime = SieveOfEratosthenes(maxi);
    let p = [];

    for (let i = 0; i < nums.length; i++) {
        if (prime[nums[i]]) {
            p.push(i);
        }
    }

    p.sort((a, b) => a - b);

    return p[p.length - 1] - p[0];
}

  const input = [4,2,9,5,3]
  const output = maximumPrimeDifference(input)
  return (
    <div>
      <p>
        <b>Input: </b>
        {JSON.stringify(input)}
      </p>
      <p>
        <b>Output:</b> {output.toString()}
      </p>
    </div>
  );
}
Result
Loading...

Complexity Analysis​

  • Time Complexity: O(nlogn)O(nlogn)
  • Space Complexity: O(n) O(n)

Code in Different Languages​

Written by @hiteshgahanolia
function SieveOfEratosthenes(n) {
let prime = Array(n + 1).fill(true);
prime[0] = prime[1] = false;

for (let p = 2; p * p <= n; p++) {
if (prime[p]) {
for (let i = p * p; i <= n; i += p) {
prime[i] = false;
}
}
}

return prime;
}

function maximumPrimeDifference(nums) {
let maxi = Math.max(...nums);
let prime = SieveOfEratosthenes(maxi);
let p = [];

for (let i = 0; i < nums.length; i++) {
if (prime[nums[i]]) {
p.push(i);
}
}

p.sort((a, b) => a - b);

return p[p.length - 1] - p[0];
}


References​