Find pairs in an array that sum to y

View previous topic View next topic Go down

Find pairs in an array that sum to y

Post  Admin on Mon Aug 29, 2011 11:00 pm

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++;
}
}

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