K Divisible Elements Subarrays - Problem

You're given an integer array nums and two integers k and p. Your task is to find the number of distinct subarrays that contain at most k elements divisible by p.

A subarray is any contiguous sequence of elements from the original array. Two subarrays are considered distinct if they have different lengths OR if there's at least one position where their elements differ.

Example: In array [2, 3, 3, 2, 2] with k=2, p=2, we need to count subarrays with at most 2 elements divisible by 2. The subarray [2, 3] starting at index 0 is different from [2, 3] starting at index 2, even though they have the same elements, because they occupy different positions in the original array.

This problem combines subarray enumeration with constraint checking and requires efficient duplicate detection using hash-based techniques.

Input & Output

example_1.py — Basic Case
$ Input: nums = [2,3,3,2,2], k = 2, p = 2
Output: 11
💡 Note: Valid subarrays with at most 2 elements divisible by 2: [2],[3],[3],[2],[2],[2,3],[3,3],[3,2],[2,2],[2,3,3],[3,3,2]. Even though some have same values, they're at different positions so they're distinct.
example_2.py — Edge Case k=0
$ Input: nums = [1,2,3,4], k = 0, p = 2
Output: 3
💡 Note: Only subarrays with 0 elements divisible by 2 are valid: [1],[3],[1,3]. All others contain at least one even number.
example_3.py — All Elements Divisible
$ Input: nums = [2,4,6], k = 1, p = 2
Output: 3
💡 Note: Each single element forms a valid subarray: [2],[4],[6]. All longer subarrays exceed k=1 limit.

Visualization

Tap to expand
K Divisible Elements Subarrays: Visual SolutionArray: [2, 3, 3, 2, 2], k=2, p=22div by 23not div3not div2div by 22div by 2Processing Steps:1. Start at index i2. Extend to index j, count divisible3. If count ≤ k, add to hash set4. If count > k, break (early stop)Hash Set (Stores Unique Subarrays)[2] [3] [3] [2] [2] [2,3] [3,3] [3,2] [2,2][2,3,3] [3,3,2] [2,3,3,2] ... (total: 11 unique)⚡ Optimization Benefits• Early termination reduces unnecessary work• Hash set provides O(1) duplicate detection• Incremental building avoids O(n) subarray copying• Time: O(n²·L), Space: O(n²·L) where L = avg length
Understanding the Visualization
1
Choose Starting Point
Pick a starting position on the shelf (array index)
2
Extend Section
Add books one by one, counting books of target genre
3
Check Constraint
If genre count ≤ k, this section is valid
4
Record Unique Sections
Use hash set to avoid counting duplicate sections
5
Early Stop
If genre count > k, stop extending (no point continuing)
Key Takeaway
🎯 Key Insight: By building subarrays incrementally and using early termination, we avoid unnecessary work while maintaining efficient duplicate detection through hashing.

Time & Space Complexity

Time Complexity
⏱️
O(n²·L)

O(n²) subarray pairs, O(L) average length for string conversion, but with early termination

n
2n
Quadratic Growth
Space Complexity
O(n²·L)

Hash set stores up to O(n²) strings, each of average length L

n
2n
Quadratic Space

Constraints

  • 1 ≤ nums.length ≤ 200
  • 1 ≤ nums[i], p ≤ 200
  • 1 ≤ k ≤ nums.length
  • All test cases are generated such that the answer is at most 104
Asked in
Google 35 Amazon 28 Meta 22 Microsoft 18
23.5K Views
Medium Frequency
~25 min Avg. Time
980 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