-
Notifications
You must be signed in to change notification settings - Fork 1
/
arrays.js
37 lines (35 loc) · 824 Bytes
/
arrays.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
function ArrayAddition(arr){
arr.sort(function (a, b) {
return a - b;
}); // step 1
var max = arr.pop(); // step 2
var sum = 0; // step 3
for (var i = 0; i < arr.length; i++) {
// step 4
sum += arr[i];
for (var j = 0; j < arr.length; j++) {
if (i !== j) {
// make sure we don't add the same element twice
sum += arr[j];
if (sum === max) {
// step 5
return true;
}
}
}
for (var k = 0; k < arr.length; k++) {
if (i !== k) {
// make sure we don't subtract the same element twice
sum -= arr[k];
if (sum === max) {
// step 5
return true;
}
}
}
sum = 0; // reset the sum
}
return false; // step 6
}
arr.split(',')
console.log(ArrayAddition([5,7,16,1,2]))