AndrewNeedsHelp
AndrewNeedsHelp

Reputation: 415

How do you test whether both sides of an array total the same? | Javascript Algorithm

Question

You are going to be given an array of integers. Your job is to take that array and find an index N where the sum of the integers to the left of N is equal to the sum of the integers to the right of N. If there is no index that would make this happen, return -1.

For example:

Let's say you are given the array {1,2,3,4,3,2,1}: Your function will return the index 3, because at the 3rd position of the array, the sum of left side of the index ({1,2,3}) and the sum of the right side of the index ({3,2,1}) both equal 6.

Answer

function findEvenIndex(arr){
  
  for(let i = 0; i <arr.length; i++){
    
       let arr1 = arr.slice(0, (arr[i] - 1));
       let arr2 = arr.slice((arr[i] + 1),);
       
      let arr11 = arr1.reduce((total, item)=>{
         return total + item;
       }, 0);

      let arr22 = arr2.reduce((total, item)=>{
         return total + item;
       }, 0);

      if(arr11 === arr22){
        return arr[i];
      }

  }

  return -1;

}


console.log(findEvenIndex([1, 2, 3, 4, 3, 2, 1]))
console.log(findEvenIndex([1, 100, 50, -51, 1, 1]))
console.log(findEvenIndex([1, 2, 3,4,5,6]))

I can't see an error here, but it yields incorrect results. Any ideas?

Upvotes: 1

Views: 404

Answers (3)

Scott Sauyet
Scott Sauyet

Reputation: 50807

You could also use the array method findIndex, which, we shouldn't be surprised to learn, finds an index in an array subject to a certain condition.

const sum = (ns) =>
  ns .reduce ((total, n) => total + n, 0)

const findBalancedIndex = (ns) => 
  ns .findIndex ((_, i) => sum (ns.slice (0, i)) === sum (ns.slice (i + 1)))


console .log (findBalancedIndex ([1, 2, 3, 4, 3, 2, 1]))
console .log (findBalancedIndex ([1, 100, 50, -51, 1, 1]))
console .log (findBalancedIndex ([1, 2, 3, 4, 5, 6]))

Here we include a simple helper function to find the sum of an array, and then we pass a function to findIndex which uses it twice on the elements before the index and those after it. We use the second parameter of the callback function, the index to do this. This means we are skipping the first parameter altogether, and rather than naming it with something like n, we use the somewhat common convention of calling it _, signalling a placeholder we won't use. Note that you don't need to subtract one from the right-hand boundary of slice, since that boundary value is already excluded. And of course, others have pointed out that you need to slice to the index and not the array value at that index.

This finds the first correct index. You would have to use a different technique if you wanted to find all such indices. (That it's possible to have more than one should be clear from arrays like [1, 2, 3, 0, 0, 0, 0, 3, 2, 1] -- the indices for all those 0s would work.)

Upvotes: 2

Martijn
Martijn

Reputation: 16113

You have this part:

   let arr1 = arr.slice(0, (arr[i] - 1));
   let arr2 = arr.slice((arr[i] + 1),);

This is incorrect: arr[i]. That is a value, eg in [2,4,6,8,10] arr[3]==8. You want to slice on the index itself:

   let arr1 = arr.slice(0, i - 1);
   let arr2 = arr.slice(i + 1,);

Please note: There is another error in the two lines :) I leave that to you. Hint: you're now slicing two values out of the array instead of one. Perform the following code in your head first, then somewhere where you verify your results.

let arr = [0,1,2,3,4]
let x = 2;
console.log(arr.slice(0, x - 1));
console.log(arr.slice(x + 1,));

Upvotes: 4

Konstantin
Konstantin

Reputation: 26

you return arr[i] when you need to return just i

Upvotes: 1

Related Questions