MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / EqualSide

Class EqualSide

Java/Array/EqualSide.java:11–33  ·  view source on GitHub ↗

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. I/P:[1,2,3,4,3,2,1] O/P: 3

Source from the content-addressed store, hash-verified

9
10
11public class EqualSide {
12 public static int findEvenIndex(int[] arr) {
13 // your code
14 int sum = 0, eq = 0;
15 for (int i = 0; i < arr.length; i++){
16 sum += arr[i];
17 }
18 for(int i = 0; i < arr.length; i++){
19 if (eq == sum - arr[i]){
20 return i;
21 }
22 eq += arr[i];
23 sum -= arr[i];
24 }
25 return -1;
26 }
27
28 public static void main(String[] args){
29 int[] array = {1,2,3,4,3,2,1};
30 int sideVal = findEvenIndex(array);
31 System.out.println(sideVal);
32 }
33}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected