-
-
Save sava-vidakovic/bbb2ab432d7ac6b512c210aca4c238cc to your computer and use it in GitHub Desktop.
// Equal Sides Of An Array | |
function findEvenIndex(arr) { | |
var sum = 0, | |
leftSum = 0; | |
for (var i = 0; i < arr.length; i++) { | |
sum += arr[i]; | |
} | |
for (var i = 0; i < arr.length; i++) { | |
sum -= arr[i]; | |
if (leftSum === sum) { | |
return i; | |
} | |
leftSum += arr[i]; | |
} | |
return -1; | |
} |
@federiccobrunelli you're welcome :)
If you need any help please let me know.
I'll for sure ! :D thanks for the offer !
could you explain the code a little bit? i dont understand what exactly for loops are doing. one adds sums other subtracts and leftsum=0 you said if leftsum===sum
Thanks for this. Would have appreciated if you included the code explanation. understood the code up until the if statement.
thanks for solution
could you explain the code a little bit? i dont understand what exactly for loops are doing. one adds sums other subtracts and leftsum=0 you said if leftsum===sum
Imagine 2 pointers (sum and leftsum), sum contains the sum of the entire array, which means we can assume it points to the end of the array. leftsum (initially), is zero and points to the start of the array.
So, now, in a for loop, we check if leftsum == sum at index i (now at start). If true, it means that the question's condition is satisfied (i.e. return the smallest index where sum of elements to the left equals sum of elements to the right). If the condition fails, then we simply add the item at index to leftsum and subtract it from total sum.
The index has increased, leftsum has a new value and sum pointer is reduced by the item at arr[index].
It's quite smart, I came up with exactly the same logic and was hoping to find an answer where a different logic was used. Guess this is the only way to solve this problem ;)
function findEvenIndex(arr) {
const reducer = (acc, curr) => acc + curr;
let leftSum = 0
let rightSum = arr.reduce(reducer, 0);
arr.forEach((num, i) => {
rightSum -= num;
if (leftSum === rightSum) {
return i;
}
leftSum += num;
})
return -1;
}
I tried refactoring this, but it won't work. Can someone help me debug this?
function findEvenIndex(arr) { const reducer = (acc, curr) => acc + curr; let leftSum = 0 let rightSum = arr.reduce(reducer, 0); arr.forEach((num, i) => { rightSum -= num; if (leftSum === rightSum) { return i; } leftSum += num; }) return -1; }
I tried refactoring this, but it won't work. Can someone help me debug this?
your forEach loop won't break when left ==right. try using the conventional loop.
function findEvenIndex(arr) { const reducer = (acc, curr) => acc + curr; let leftSum = 0 let rightSum = arr.reduce(reducer, 0); arr.forEach((num, i) => { rightSum -= num; if (leftSum === rightSum) { return i; } leftSum += num; }) return -1; }
I tried refactoring this, but it won't work. Can someone help me debug this?
Here's a working version of it
function findEvenIndex(arr) {
let leftSum = 0;
let rightSum = arr.reduce((acc, curr) => acc + curr, 0) - arr[0];
if (rightSum === 0) return 0;
for (let i = 0; i < arr.length - 1; i++) {
leftSum += arr[i];
rightSum -= arr[i+1];
if (leftSum === rightSum){
return i + 1;
};
}
if (arr.reduce((acc, curr) => acc + curr, 0) - arr[arr.length - 1] === 0) return arr.length - 1;
if (!rightSum) return -1;
return -1;
}
Thanks dude ! I couldn't find a way by myself !