Last active
March 31, 2023 06:22
-
-
Save sava-vidakovic/bbb2ab432d7ac6b512c210aca4c238cc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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; | |
| } |
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;
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@lowkeyboard
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 ;)