C++ Solution using Binary Array - Two Pointers and Sliding Window
Approach
Create a set of vectors to store the unique/distinct subarrays.
Convert the vector into a binary vector with 1’s representing elements divisible by p a

# Approach
<!-- Describe your approach to solving the problem. -->
- Create a set of vectors to store the unique/distinct subarrays.
- Convert the vector into a binary vector with 1’s representing elements divisible by p and 0’s representing elements not divisible by p.
- Using Two Pointers ‘left’ and ‘right’ iterate over the binary vector and keep count of the 1’s in the array.
- if count exceeds k, stop further execution.
- Insert the subarray into the set.
- Return the size of the set.

# Complexity
- Time complexity: O(N)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(N)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Dry Run :-

nums = [2,3,3,2,2], k = 2, and let's assume p = 2 for this example.

Let's go step by step:

1. First, we create A[] array marking numbers divisible by p(2):
    
    ```
    Copy
    nums = [2,3,3,2,2]
    A =    [1,0,0,1,1]  // 1 for numbers divisible by 2
    
    ```
    
2. Now let's iterate through each left position and generate subarrays:

For left = 0:

```
Copy
[2]           - countDivisible=1, add to set
[2,3]         - countDivisible=1, add to set
[2,3,3]       - countDivisible=1, add to set
[2,3,3,2]     - countDivisible=2, add to set
[2,3,3,2,2]   - countDivisible=3, break (>k)

```

For left = 1:

```
Copy
[3]           - countDivisible=0, add to set
[3,3]         - countDivisible=0, add to set
[3,3,2]       - countDivisible=1, add to set
[3,3,2,2]     - countDivisible=2, add to set

```

For left = 2:

```
Copy
[3]           - countDivisible=0, add to set
[3,2]         - countDivisible=1, add to set
[3,2,2]       - countDivisible=2, add to set

```

For left = 3:

```
Copy
[2]           - countDivisible=1, add to set
[2,2]         - countDivisible=2, add to set

```

For left = 4:

```
Copy
[2]           - countDivisible=1, add to set

```

Let's count unique subarrays:

- [2]
- [2,3]
- [2,3,3]
- [2,3,3,2]
- [3]
- [3,3]
- [3,3,2]
- [3,3,2,2]
- [3,2]
- [3,2,2]
- [2,2]

The answer would be 11 as there are 11 unique subarrays where the count of numbers divisible by 2 is ≤ 2.

Note that even though we generated some subarrays multiple times (like [2]), the set data structure ensures we only count unique subarrays once.

# Code
```cpp []
class Solution {
public:
    int countDistinct(vector<int>& nums, int k, int p) {
        set<vector<int>> uniqueSubarrays;
        vector<int> A(nums.size(), 0);
        for (int i = 0; i < nums.size(); i++) {
            if (nums[i] % p == 0) {
                A[i] = 1;
            }
        }

        int n = nums.size();

        for (int left = 0; left < n; left++) {
            vector<int> subarray;
            int countDivisible = 0;

            for (int right = left; right < n; right++) {
                subarray.push_back(nums[right]); 
                countDivisible += A[right];

                if (countDivisible > k) {
                    break; 
                }

                uniqueSubarrays.insert(subarray);
            }
        }

        return uniqueSubarrays.size();
    }
};

```

['C++']