The task is to write a program that returns the index of the cell in the array where the sum of the elements in the array until that cell's element equals the sum of the elements after it until the end. This is for any size array.

If there is no index like that, return -1.

for example: 2 1 6 5 4 returns 2 since 2+1+6=5+4 and 6 is in place 2

however: 3 2 7 returns -1...

Attempt:

I tried writing a program that would check the array like this...

Code:`2+1+6+5 =4,`

2+1+6=5+4,

2+1=6+5+4,

2=1+6+5+4.

Can anyone find the error here (or most likely errors...)?Code:`public class EqualSum`

{

public static int equalSum(int[]a){

int counter=0; // counter saves the values of a[j]

for (int j=a.length-1; j>0; j--){

if (sum(a, a[j-1]) == (a[j] + counter)) // this checks if the sum from the first cell through the j-1 cell equals the j cell. Since a[j] gets 1 smaller every time, counter keeps the values of the previous a[j]'s...

return j-1;

counter += a[j];

}

return -1;

}

public static int sum(int[]array, int until){

int counter=0;

for (int i=0; i<until; i++)

counter+= array[i];

return counter;

}

}

Thanks.