Skip to content

Latest commit

 

History

History
30 lines (24 loc) · 722 Bytes

k-divisible-elements-subarray.md

File metadata and controls

30 lines (24 loc) · 722 Bytes

K Divisible Elements Subarray

class Solution {
public:
    int countDistinct(vector<int>& nums, int k, int p) { 
        
        int P = 211, N = nums.size();
        unordered_set<unsigned long long>S;
        
        for (int i = 0; i < N; i++) {
            unsigned long long h = 0;
            int countDivisible = 0;
            
            for (int j = i; j < N; j++) {
                h = h * P + nums[j];
                countDivisible += (nums[j] % p == 0);
                
                if (countDivisible <= k) {
                    S.insert(h);
                } else {
                    break;
                }
            }
        }
        
        return (int)(S.size());
    }
};