-
Notifications
You must be signed in to change notification settings - Fork 0
/
rStrSub.js
68 lines (63 loc) · 1.34 KB
/
rStrSub.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
function rStrSub(str, sub='', arr=[]){
if(str.length === 0){
arr.push(sub);
} else {
rStrSub(str.slice(1), sub, arr);
sub += str[0];
rStrSub(str.slice(1), sub, arr);
}
return arr;
}
var result = rStrSub('abc');
console.log(result);
function useStringSub(arr){
arr = arr.join('');
arr = rStrSub(arr);
for(i in arr){
arr[i] = arr[i].split('');
}
return arr;
}
// console.log(useStringSub([1,2,3]));
function rArrSub(arr, sub, output){
output = output || [];
sub = sub || [];
sub2 = sub || [];
if(arr.length === 0){
output.push(sub.slice(0));
} else {
newArr = arr.slice(1)
newArr2 = arr.slice(1)
rArrSub(newArr, sub.slice(0), output);
sub2.push(arr[0]);
rArrSub(newArr2, sub2.slice(0), output);
}
return output;
}
// var arr = [1,7,2,4];
// result3 = hackerRank(arr, 3);
// console.log(result3);
function hackerRank(arr, x){
arr = rArrSub(arr);
console.log('arr: ', arr);
var length = 0;
for(let i = 0; i < arr.length; i++){
check = true;
for(let j = 0; j < arr[i].length; j++){
for(let k = j + 1; k < arr[i].length; k++){
console.log('sum: ', arr[i][j] + arr[i][k])
if((arr[i][j] + arr[i][k]) % x == 0){
check = false;
}
console.log('check: ', check);
}
}
console.log('-- next array -- ')
if(check){
if(arr[i].length > length){
length = arr[i].length;
}
}
}
return length;
}