forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathK Divisible Elements Subarrays.js
43 lines (37 loc) · 1.01 KB
/
K Divisible Elements Subarrays.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Runtime: 3190 ms (Top 8.57%) | Memory: 86.8 MB (Top 45.72%)
var countDistinct = function(nums, k, p) {
let ans = [];
let rec = (index,k,p,nums,ans,curr) => {
let val = nums[index];
let check = val%p;
let isdiv = false;
if(check == 0) isdiv=true;
if(index == nums.length) {
if(curr.length>0){
ans.push(curr.join(","));
}
return;
}
//take conditions
if(isdiv && k==0){
ans.push(curr.join(","));
} else if(isdiv){
curr.push(val)
rec(index+1,k-1,p,nums,ans,curr);
curr.pop()
} else {
curr.push(val)
rec(index+1,k,p,nums,ans,curr);
curr.pop()
}
//non take conditions
if(curr.length == 0){
rec(index+1,k,p,nums,ans,curr);
} else {
ans.push(curr.join(","));
}
}
rec(0,k,p,nums,ans,[]);
let set = new Set(ans);
return set.size
};