-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanBalanceSolution.js
44 lines (34 loc) · 1.27 KB
/
canBalanceSolution.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
//canBalance function takes an array of numbers and returns true or false based on the following conditions
//It returns true if the array can be split at any point with the sum of the numbers on one side equal to the sum of the numbers at the other side
// For example, given the array [1, 2, 3, 4, 5, 5], it will return true as the array can be split into [1, 2, 3, 4] and [5, 5] giving a sum of 10 on each side.
function canBalance(array) {
let leftArray = [],
rightArray = [],
leftArrayTotal = 0,
rightArrayTotal = 0,
totalValues = array.reduce((acc, val)=>acc+val,0);
let leftArray = [];
//let rightArray = [];
//let leftArrayTotal = 0;
let rightArrayTotal = 0;
let totalValues = array.reduce((acc, val)=>acc+val,0)
if (totalValues%2 == 0 && totalValues>0){
let done = totalValues/2;
for(let i = 0; i<array.length;){
leftArrayTotal += array[0];
leftArray.push(array.shift());
if(checker === leftArrayTotal){
rightArray = array;
rightArrayTotal = rightArray.reduce((acc, val)=>acc+val,0)
break;
}
}
if(leftArrayTotal === rightArrayTotal){
return ([leftArray.length, rightArray.length])
}
else{return -1}
}
else{return -1;}
}
canBalance([1, 2, 3, 4, 5, 6, 6, 7, 8])
module.exports = canBalance;