You are given an integer array nums and an integer k.
A subarray is called prime-gap balanced if:
k.Return the count of prime-gap balanced subarrays in nums.
Note:
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 * 1041 <= nums[i] <= 5 * 1040 <= k <= 5 * 104Problem Overview: You are given an integer array and need to count subarrays where the gaps between consecutive prime elements remain balanced. A prime gap is the distance between indices of adjacent prime numbers inside the subarray. The task is to efficiently count all subarrays where these gaps satisfy the balancing condition.
Approach 1: Brute Force with Prime Tracking (O(n²) time, O(n) space)
Enumerate every possible subarray using two nested loops. While expanding the right boundary, maintain a list of indices where elements are prime. Each time a new prime appears, compute the gap between it and the previous prime. Track the minimum and maximum gap seen in the current subarray and check if the balancing condition holds. This method directly simulates the definition but recomputes gaps frequently, making it too slow for large inputs.
Approach 2: Sliding Window + Monotonic Queues (O(n) time, O(n) space)
First determine which elements are prime using a fast primality check or a sieve from number theory. As you scan the array, maintain a sliding window over indices. Each time you encounter a prime, compute the gap between this index and the previous prime in the window. Store these gaps in two monotonic queues: one increasing queue for the minimum gap and one decreasing queue for the maximum gap.
While expanding the window, update both queues with the new gap. If the window becomes invalid (the maximum gap differs from the minimum gap), move the left pointer forward and remove outdated gaps from the queues. Because each gap enters and leaves the queues once, the total work is linear. This technique combines a sliding window with a monotonic queue to maintain range statistics in constant time.
The key insight is that the condition depends only on prime gaps, not the entire subarray. By maintaining the min and max gap incrementally, you avoid recomputing values for every subarray. Each pointer moves at most n times, so the scan remains efficient even for large arrays.
Recommended for interviews: Interviewers expect the sliding window with monotonic queues. The brute force approach shows you understand how prime gaps are defined and how subarrays are enumerated. The optimized window demonstrates mastery of range tracking inside a dynamic window, a common pattern in advanced array problems.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force with Prime Tracking | O(n²) | O(n) | Useful for understanding the definition of prime gaps and verifying correctness on small inputs |
| Sliding Window + Monotonic Queues | O(n) | O(n) | Best general solution when scanning large arrays and maintaining min/max gap constraints |
3589. Count Prime-Gap Balanced Subarrays | C++ | Sliding Window • BEASTCODES • 719 views views
Watch 1 more video solutions →Practice Count Prime-Gap Balanced Subarrays with our built-in code editor and test cases.
Practice on FleetCode