Skip to content

Instantly share code, notes, and snippets.

@sava-vidakovic
Last active March 31, 2023 06:22
Show Gist options
  • Save sava-vidakovic/bbb2ab432d7ac6b512c210aca4c238cc to your computer and use it in GitHub Desktop.
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
Copy link

Thanks dude ! I couldn't find a way by myself !

@sava-vidakovic
Copy link
Author

@federiccobrunelli you're welcome :)
If you need any help please let me know.

@federiccobrunelli
Copy link

I'll for sure ! :D thanks for the offer !

@lowkeyboard
Copy link

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

@Babadinho
Copy link

Babadinho commented Apr 23, 2020

Thanks for this. Would have appreciated if you included the code explanation. understood the code up until the if statement.

@cihat
Copy link

cihat commented Sep 12, 2020

thanks for solution

@ninjaasmoke
Copy link

ninjaasmoke commented Apr 22, 2021

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

@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 ;)

@filipeqm94
Copy link

filipeqm94 commented Apr 28, 2021

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?

@coffee709
Copy link

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.

@younes-benniz
Copy link

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