# Find index that divides an array into two subarrays of equal sum

Given an array A of length n, the task is to find the index that divides the array into two subarrays of equal sum. If the array can not be divided in equal sum the method should return -1.

We can solve this problem in O(n) time and O(1) extra space, the idea is to first calculate total sum of the array, than start iterating the array from left to keep track of sum_so_far, at any index if sum_so_far is equal to total- (arr[i]+sum_so_far), we found the dividing index.

```package com.cd.array;

public class SubarrayEqualSum {

private static int[] arr = {-1, 6, 3, 1, -2, 3, 3};

private static int divider(int[] arr) {
int totalSum = 0;

for (int i = 0; i < arr.length; i++) {
totalSum += arr[i];
}

int leftSum = arr;

for (int i = 1; i < arr.length; i++) {

if (leftSum == totalSum - (arr[i] + leftSum))
return i;

leftSum += arr[i];

}
return -1;
}

public static void main(String[] args) {
System.out.println("Dividing Index: " + divider(arr));
}
}
```
Output: The output of above example will look something like this.
```Dividing Index: 2
```