/**
* Find the index in an array where sum to left is equal to sum to its right
* @author PRATEEK
*/
class EquilibriumIndex {
/**
* @param arr: input arr
* @return : equilibrium index in the array
*/
private int equilibrium(int[] arr) {
int i = 0, sum = 0, rightSum = 0;
for (; i < arr.length; sum += arr[i++]);
for (i--; i > 0; sum -= arr[i], rightSum += arr[i])
if (sum == rightSum - arr[i--])
return i;
return -1;
}
public static void main
(String[] args
) {
int arr[] = { -7, 1, 6, 2, -4, 3, 1 };
EquilibriumIndex obj = new EquilibriumIndex();
int mid = obj.equilibrium(arr);
}
}
LyoqCiAqIEZpbmQgdGhlIGluZGV4IGluIGFuIGFycmF5IHdoZXJlIHN1bSB0byBsZWZ0IGlzIGVxdWFsIHRvIHN1bSB0byBpdHMgcmlnaHQKICogQGF1dGhvciBQUkFURUVLCiAqLwpjbGFzcyBFcXVpbGlicml1bUluZGV4IHsKCgkvKioKCSAqIEBwYXJhbSBhcnI6IGlucHV0IGFycgoJICogQHJldHVybiA6IGVxdWlsaWJyaXVtIGluZGV4IGluIHRoZSBhcnJheQoJICovCglwcml2YXRlIGludCBlcXVpbGlicml1bShpbnRbXSBhcnIpIHsKCQlpbnQgaSA9IDAsIHN1bSA9IDAsIHJpZ2h0U3VtID0gMDsKCQlmb3IgKDsgaSA8IGFyci5sZW5ndGg7IHN1bSArPSBhcnJbaSsrXSk7CgoJCWZvciAoaS0tOyBpID4gMDsgc3VtIC09IGFycltpXSwgcmlnaHRTdW0gKz0gYXJyW2ldKQoJCQlpZiAoc3VtID09IHJpZ2h0U3VtIC0gYXJyW2ktLV0pCgkJCQlyZXR1cm4gaTsKCgkJcmV0dXJuIC0xOwoJfQoKCXB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKCgkJaW50IGFycltdID0geyAtNywgMSwgNiwgMiwgLTQsIDMsIDEgfTsKCQlFcXVpbGlicml1bUluZGV4IG9iaiA9IG5ldyBFcXVpbGlicml1bUluZGV4KCk7CgkJaW50IG1pZCA9IG9iai5lcXVpbGlicml1bShhcnIpOwoJCVN5c3RlbS5vdXQucHJpbnRsbihtaWQpOwoKCX0KfQo=