Minimum Operations to Transform Array into Alternating Prime

You are given an integer array nums.

An array is considered alternating prime if:

  • Elements at even indices (0-based) are prime numbers.
  • Elements at odd indices are non-prime numbers.

In one operation, you may increment any element by 1.

Return the minimum number of operations required to transform nums into an alternating prime array.

A prime number is a natural number greater than 1 with only two factors, 1 and itself.

Example 1
Inputnums = [1,2,3,4]
Output3
Increment nums[0] from 1 to 2 using 1 operation and nums[1] from 2 to 4 using 2 operations, while the other elements already satisfy the required parity positions.
Example 2
Inputnums = [5,6,7,8]
Output0
The elements at indices 0 and 2 are already prime, and the elements at indices 1 and 3 are already non-prime, so no operations are needed.

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^5

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate