K Divisible Elements Subarrays LeetCode solution in Python
1class Solution:2def countDistinct(self, nums: List[int], k: int, p: int) -> int:3n = len(nums)4sub_arrays = set()56# generate all combinations of subarray7for start in range(n):8cnt = 09temp = ""10for i in range(start, n):11if nums[i] % p == 0:12cnt += 113temp += str(nums[i]) + "," # build the sequence subarray in CSV format14if cnt > k: # check for termination15break16sub_arrays.add(temp)1718return len(sub_arrays)