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
Input
nums = [1,2,3,4]Output
3Increment
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
Input
nums = [5,6,7,8]Output
0The 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