-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanSum.js
36 lines (32 loc) · 922 Bytes
/
canSum.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
//this program is for checking if the number can be obtained by summing the required elements in the given array
const canSum = (n, array, memo = []) => {
if(n in memo) return memo[n];
if(n === 0) return true;
if(n < 0) return false; //time: O(n * m)
space: O(m)
for(let num of array)
{
if(canSum(n-num, array, memo)){
return memo[n] = true;
}
}
return memo[n] = false;
};
console.log(canSum(7, [7, 5, 3, 4]));
//this program is for obtaining the array of numbers falling in the given array that sum to give the given number
const sumArray = (n, array, memo = [])=> {
if(n in memo) return memo[n];
if(n === 0) return a;
if(n < 0) return null;
for(let num of array) //time: O(n * m ^ 2)
{ //space: O(m)
a.push(num);
if(canSum(n-num, array, memo)){
return memo[n] = a;
}
a.pop();
}
return memo[n] = null;
};
let a = [];
console.log(sumArray(0, [7, 14]));