-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminSum.js
65 lines (47 loc) · 1.59 KB
/
minSum.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
// Introduction and Warm-up (Highly recommended)
// Playing With Lists/Arrays Series
// Task
// Given an array of integers , Find the minimum sum which is obtained from summing each Two integers product .
// Notes
// Array/list will contain positives only .
// Array/list will always has even size
// Input >> Output Examples
// minSum({5,4,2,3}) ==> return (22)
// Explanation:
// The minimum sum obtained from summing each two integers product , 5*2 + 3*4 = 22
// minSum({12,6,10,26,3,24}) ==> return (342)
// Explanation:
// The minimum sum obtained from summing each two integers product , 26*3 + 24*6 + 12*10 = 342
// minSum({9,2,8,7,5,4,0,6}) ==> return (74)
// Explanation:
// The minimum sum obtained from summing each two integers product , 9*0 + 8*2 +7*4 +6*5 = 74
// Playing with Numbers Series
// Playing With Lists/Arrays Series
// For More Enjoyable Katas
// ALL translations are welcomed
// Enjoy Learning !!
// Zizou
function minSum(arr) {
let sorted = arr.sort((a,b) => a-b);
let sum = 0;
for(let i = 0; i < sorted.length/2; i++){
sum += sorted[i] * sorted[sorted.length - i - 1];
}
return sum;
}
console.log(minSum([12,6,10,26,3,24]));
//cw's
function minSum2(arr) {
return arr.sort( (a,b) => a-b )
.slice(0, arr.length/2)
.reduce( (acc,curr,index) => acc += curr * arr[ arr.length - index - 1 ], 0 )
}
function minSum3(arr) {
arr.sort((x,y)=>x-y)
s=0
while(arr.length)s+=arr.pop()*arr.shift()
return s
}
function minSum4(a) {
return a.sort((a,b)=>a-b).reduce((s,x,i,a)=>s+x*a[a.length-i-1],0)/2
}