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.
Page 1 of 1
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.
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;
}
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;
}
Similar topics
» Given a sorted array find all possible |ai - aj| where ai,aj belongs to Array A. n^2 is obvious. Find a solution in O(N).
» Find duplicate words in a string
» Given a document, how to find pairs of words with same charactors but different order.
» Find pairs in an array that sum to y
» Find the n-th largest int in an array
» Find duplicate words in a string
» Given a document, how to find pairs of words with same charactors but different order.
» Find pairs in an array that sum to y
» Find the n-th largest int in an array
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|