arr 13 24 11 18 15 10 function solve arr arr arr sort function return

 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
arr = [4,6,13,24,11,7,18,2,5,3,15,10]
function solve(arr) {
arr = arr.sort(function(a,b){return b-a})
sums = []
for (i1 = 0; i1 < arr.length - 1; i1++) {
for (i2 = i1 + 1; i2 < arr.length; i2++) {
sums.push(arr[i1] + arr[i2])
}
}
sums = sums.sort(function(a,b){return b-a})
console.log(sums)
for (i = 0; i < sums.length; i++) {
if (i != 0 && sums[i] == sums[i-1])
continue
sum = sums[i]
for (n1 = 0; n1 < arr.length - 2; n1++) {
for (n2 = n1 + 1; n2 < arr.length - 1; n2++) {
if (sum - arr[n1] - arr[n2] <= 0)
continue
for (n3 = n2 + 1; n3 < arr.length - 1; n3++) {
if (sum == arr[n1] + arr[n2] + arr[n3]) {
for (m1 = 0; m1 < arr.length - 4; m1++) {
for (m2 = m1 + 1; m2 < arr.length - 3; m2++) {
if (sum - arr[m1] - arr[m2] <= 0)
continue
for (m3 = m2 + 1; m3 < arr.length - 2; m3++) {
if (sum - arr[m1] - arr[m2] - arr[m3] <= 0)
continue
for (m4 = m3 + 1; m4 < arr.length - 1; m4++) {
if (sum - arr[m1] - arr[m2] - arr[m3] - arr[m4] <= 0)
continue
for (m5 = m4 + 1; m5 < arr.length - 0; m5++) {
if (sum == arr[m1] + arr[m2] + arr[m3] + arr[m4] + arr[m5]) {
console.log(arr[n1] , arr[n2] , arr[n3])
console.log(arr[m1] , arr[m2] , arr[m3] , arr[m4] , arr[m5])
return sum
}
}
}
}
}
}
}
}
}
}
}
}
console.log(solve(arr))