forked from Adespinoza/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_042.js
33 lines (29 loc) Β· 880 Bytes
/
problem_042.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
/**
* Finds subset of an array that equals to k
* @param {number[]} array
* @param {number} k
* @return {number[]}
* O(nLogn) Time Complexity
* O(n) Space Complexity
*/
function getSubsetSum(array, k) {
//Base cases
if(array.length === 0) return null;
const sortedArr = array.sort((a,b) => b - a);
const subset = [];
let remainingSum = k;
// Iterate through array until sum reached
for (let i = 0; i < sortedArr.length; i++) {
if(sortedArr[i] <= remainingSum) {
subset.push(sortedArr[i]);
remainingSum -= sortedArr[i];
}
}
if(remainingSum === 0) return subset;
return null;
}
console.log(getSubsetSum([12, 1, 61, 5, 9, 2], 24)); // [12, 9, 2, 1]
console.log(getSubsetSum([12, 1], 24)); // null
console.log(getSubsetSum([12, 12], 24)); // [12, 12]
console.log(getSubsetSum([], 24)); // null
console.log(getSubsetSum([24], 24)); // [24]