-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchef.js
More file actions
27 lines (22 loc) · 696 Bytes
/
Copy pathchef.js
File metadata and controls
27 lines (22 loc) · 696 Bytes
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
function weightDifference(N, K, weights) {
let numberOfItemsCarriedByChef = N - K;
let sortedWeights = weights.sort((a, b) => b - a);
let weightsCarriedByChef = sortedWeights
.slice(0, numberOfItemsCarriedByChef)
.reduce((current, next) => current + next, 0);
let weightsCarriedByKid = sortedWeights
.slice(numberOfItemsCarriedByChef, N)
.reduce((current, next) => current + next, 0);
return weightsCarriedByChef - weightsCarriedByKid;
}
let testCase = [
{
N: 5,
K: 2,
weights: [8, 4, 5, 2, 10],
},
{ N: 8, K: 3, weights: [1, 1, 1, 1, 1, 1, 1, 1] },
];
testCase.forEach(({ N, K, weights }) => {
console.log(weightDifference(N, K, weights));
});