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
Visualization
Time & Space Complexity
O(n²) subarray pairs, O(L) average length for string conversion, but with early termination
Hash set stores up to O(n²) strings, each of average length L
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