Description
You are given an integer array nums
and an integer k
.
A subarray is called prime-gap balanced if:
- It contains at least two prime numbers, and
- The difference between the maximum and minimum prime numbers in that subarray is less than or equal to
k
.
Return the count of prime-gap balanced subarrays in nums
.
Note:
- A subarray is a contiguous non-empty sequence of elements within an 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], k = 1
Output: 2
Explanation:
Prime-gap balanced subarrays are:
[2,3]
: contains two primes (2 and 3), max - min =3 - 2 = 1 <= k
.[1,2,3]
: contains two primes (2 and 3), max - min =3 - 2 = 1 <= k
.
Thus, the answer is 2.
Example 2:
Input: nums = [2,3,5,7], k = 3
Output: 4
Explanation:
Prime-gap balanced subarrays are:
[2,3]
: contains two primes (2 and 3), max - min =3 - 2 = 1 <= k
.[2,3,5]
: contains three primes (2, 3, and 5), max - min =5 - 2 = 3 <= k
.[3,5]
: contains two primes (3 and 5), max - min =5 - 3 = 2 <= k
.[5,7]
: contains two primes (5 and 7), max - min =7 - 5 = 2 <= k
.
Thus, the answer is 4.
Β
Constraints:
1 <= nums.length <= 5 * 104
1 <= nums[i] <= 5 * 104
0 <= k <= 5 * 104
Solution
Python3
from sortedcontainers import SortedList
def generate_primes(n):
isPrime = [True] * (n + 1)
isPrime[0] = isPrime[1] = False
for i in range(2, n + 1):
if isPrime[i]:
for j in range(i * i, n + 1, i):
isPrime[j] = False
return isPrime
PRIMES = generate_primes(50001)
class Solution:
def primeSubarray(self, nums: List[int], k: int) -> int:
N = len(nums)
i = res = 0
sl = SortedList()
queue = deque()
for j, x in enumerate(nums):
if PRIMES[x]:
sl.add(x)
queue.append(j)
while len(sl) >= 2 and sl[-1] - sl[0] > k:
pi = queue.popleft()
sl.remove(nums[pi])
i = pi + 1
if len(sl) >= 2:
lastSecond = queue[-2]
res += lastSecond - i + 1
return res