Find a point in an array where sum of left side array members(wrt to that point) and right side(wrt to that point) are equal..in other words equilibrium point.

View previous topic View next topic Go down

Find a point in an array where sum of left side array members(wrt to that point) and right side(wrt to that point) are equal..in other words equilibrium point.

Post  Admin on Thu Aug 25, 2011 4:26 pm

public static int FindPoint(int[] arr){
int len=arr.length()-1;
int i=0;int j=len;
int left=0;
int right=0;
while(i<j){
if(left>right+arr[j]){
right+=arr[j--];
}
else if (right>left+arr[i]){
left+=arr[i++];
}
else{
return i;

}
}
return 0;
}

Admin
Admin

Posts : 131
Join date : 2011-08-16

View user profile http://codefornongeek.forumotion.com

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum