Find pairs in an array that sum to y
Page 1 of 1
Find pairs in an array that sum to y
public static void FindPair(int[] arr, int y){
arr=Array.sort(arr);
int len=arr.length();
int j=len-1;
while(i<j){
int sum=arr[i]+arr[j];
if(y==sum){
System.out.println("A pair: "+ arr[i]+" and "+arr[j]);
i++;
j--;
}
else if(sum>y)
j--;
else
i++;
}
}
arr=Array.sort(arr);
int len=arr.length();
int j=len-1;
while(i<j){
int sum=arr[i]+arr[j];
if(y==sum){
System.out.println("A pair: "+ arr[i]+" and "+arr[j]);
i++;
j--;
}
else if(sum>y)
j--;
else
i++;
}
}
Similar topics
» 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.
» Given a document, how to find pairs of words with same charactors but different order.
» Find the n-th largest int in an array
» Interviewer gave me an array of character and asked me to find first non-repeating character
» Find the number of inversions in an array
» Given a document, how to find pairs of words with same charactors but different order.
» Find the n-th largest int in an array
» Interviewer gave me an array of character and asked me to find first non-repeating character
» Find the number of inversions in an array
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|