Count Prime-Gap Balanced Subarrays - Problem

Imagine you're analyzing data sequences where prime numbers serve as key landmarks. Your task is to find all contiguous subsequences (subarrays) that are "prime-gap balanced" - meaning they contain at least two prime numbers with a controlled gap between them.

Given an integer array nums and an integer k, count how many subarrays satisfy these conditions:

  • šŸ“Š Contains at least two prime numbers
  • šŸ“ The difference between the largest and smallest prime in that subarray is ≤ k

Example: In array [2, 4, 3, 5, 7] with k = 4, the subarray [2, 4, 3, 5] is prime-gap balanced because it contains primes 2, 3, 5 and max(2,3,5) - min(2,3,5) = 5 - 2 = 3 ≤ 4.

Note: A prime number is a natural number > 1 with exactly two factors: 1 and itself.

Input & Output

example_1.py — Basic Case
$ Input: nums = [2, 4, 3, 5, 7], k = 4
› Output: 6
šŸ’” Note: Valid subarrays: [2,4,3] (primes: 2,3, gap=1≤4), [2,4,3,5] (primes: 2,3,5, gap=3≤4), [2,4,3,5,7] (primes: 2,3,5,7, gap=5>4 āŒ), [4,3,5] (primes: 3,5, gap=2≤4), [4,3,5,7] (primes: 3,5,7, gap=4≤4), [3,5,7] (primes: 3,5,7, gap=4≤4). Count = 6.
example_2.py — Small Gap
$ Input: nums = [2, 8, 3, 9, 5], k = 2
› Output: 3
šŸ’” Note: Valid subarrays: [2,8,3] (primes: 2,3, gap=1≤2), [8,3,9,5] (primes: 3,5, gap=2≤2), [3,9,5] (primes: 3,5, gap=2≤2). Other subarrays either have gap>2 or <2 primes.
example_3.py — No Valid Subarrays
$ Input: nums = [4, 6, 8, 9], k = 5
› Output: 0
šŸ’” Note: Array contains no prime numbers (4=2Ɨ2, 6=2Ɨ3, 8=2Ɨ4, 9=3Ɨ3), so no subarray can have ≄2 primes.

Visualization

Tap to expand
Prime-Gap Balanced Subarray Visualization2PRIME4COMPOSITE3PRIME5PRIME7PRIMECurrent Window: [2, 4, 3, 5]Analysis k=4:āœ“ Contains ≄2 primes: 2, 3, 5āœ“ Gap = max(2,3,5) - min(2,3,5) = 5-2 = 3 ≤ 4→ This subarray is PRIME-GAP BALANCED!Key Insight:The sliding window approach efficiently tracks prime ranges withoutredundant calculations, using early termination when gap exceeds k.Time: O(n²), Space: O(n) - Much better than naive O(n³) approach!
Understanding the Visualization
1
Identify Peaks
Mark all prime numbers (peaks) in the array
2
Scan Sections
Use sliding window to examine each possible section
3
Measure Gaps
Check if elevation difference between highest and lowest peaks ≤ k
4
Count Valid
Increment counter for each section meeting the criteria
Key Takeaway
šŸŽÆ Key Insight: Precompute prime flags and use sliding window with early termination for optimal efficiency

Time & Space Complexity

Time Complexity
ā±ļø
O(n³)

O(n²) for generating subarrays Ɨ O(n) for prime checking and gap calculation

n
2n
⚠ Quadratic Growth
Space Complexity
O(1)

Only using constant extra space for variables

n
2n
āœ“ Linear Space

Constraints

  • 1 ≤ nums.length ≤ 1000
  • 1 ≤ nums[i] ≤ 1000
  • 0 ≤ k ≤ 1000
  • All array elements are positive integers
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
23.4K Views
Medium Frequency
~25 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
šŸ’” Explanation
AI Ready
šŸ’” Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen