Given an array A[] and a number x, check for pair in A[] with sum as x
given an array A[] of n numbers and another number x, determines whether
or not there exist two elements in S whose sum is exactly x.
Code:
package microsoftInterviewPrep;
import java.util.Arrays;
public class KeyPair {
static void findKeyPair(int[] a, int sum){
Arrays.sort(a);
int l = 0;
int r = a.length -1;
while(l < r){
int currsum = a[l]+ a[r];
if(currsum == sum){
System.out.println("The pairs with sum "+ sum+" are: "+ a[l]+" and "+a[r]);
break;
}
else if( currsum < sum){
l++;
}
else{
r--;
}
}
}
public static void main(String[] args){
int a[] = {1, 4, 45, 6, 10, -8};
int n = 16;
findKeyPair(a, n);
}
}
Output:
The pairs with sum 16 are: 6 and 10
Code:
package microsoftInterviewPrep;
import java.util.Arrays;
public class KeyPair {
static void findKeyPair(int[] a, int sum){
Arrays.sort(a);
int l = 0;
int r = a.length -1;
while(l < r){
int currsum = a[l]+ a[r];
if(currsum == sum){
System.out.println("The pairs with sum "+ sum+" are: "+ a[l]+" and "+a[r]);
break;
}
else if( currsum < sum){
l++;
}
else{
r--;
}
}
}
public static void main(String[] args){
int a[] = {1, 4, 45, 6, 10, -8};
int n = 16;
findKeyPair(a, n);
}
}
Output:
The pairs with sum 16 are: 6 and 10
Comments
Post a Comment