JAVA | Set | Hashing | 2 For loops | Clean and Concise
\nclass Solution {\n    public int countDistinct(int[] nums, int k, int p) {\n        int n = nums.length;\n\t\t// we are storing hashcode for all the substring
```\nclass Solution {\n    public int countDistinct(int[] nums, int k, int p) {\n        int n = nums.length;\n\t\t// we are storing hashcode for all the substrings so that we can compare them faster.\n\t\t// main goal is to avoid entire sub array comparision using hashcode.\n        Set<Long> ways = new HashSet<>(); \n        for(int i = 0; i < n; i++) {\n            int cnt = 0;\n            long hc = 1; // this is the running hashcode for sub array [i...j]\n            for(int j = i; j < n; j++) {\n                hc = 199L * hc + nums[j]; // updating running hashcode, since we nums are <=200, we shall consider a prime near 200 to avoid collision\n                if(nums[j] % p == 0)\n                    cnt++;\n                if(cnt <= k) { // if current subarray [i...j] is valid, add its hashcode in our storage.\n                    ways.add(hc);\n                }\n            }\n        }\n        return ways.size();\n    }\n}\n```
['Java']