2261 K делимых элементов

class Solution {
    public int countDistinct(int[] nums, int k, int p) {
        // k is number divisible elements
        // p is pivot
        Set<Long> numSubarray = new HashSet<>();

        for (int i = 0; i < nums.length; i++) {
            int count_div = 0;
            long hashCode = 1;
            for (int j = i; j < nums.length; j++) {
                hashCode = 199L * hashCode + nums[j];
                if (nums[j] % p == 0) {
                    ++count_div;
                }
                if (count_div <= k) {
                    numSubarray.add(hashCode);
                } else {
                    break;
                }
            }
        }
        return numSubarray.size();
    }
}
Dheeraj Sehrawat