Given an array S of n integers, Write a Java Program to find out whether there exists a pair of integers in S whose sum is equal to a given integer x.
Given an array S of n integers, Write a Java Program to find out whether there exists a pair of integers in S whose sum is equal to a given integer x.
import java.util.Scanner;
class pairSum{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number : ");
int n = sc.nextInt();
int a[]={1,2,3,6,10};
checkPair(a, n);
sc.close();
}
public static void checkPair(int A[],int x){
for(int i=0;i<(A.length-1);i++){
for(int j=i+1;j<A.length;j++){
if(A[i]+A[j]==x)
System.out.println("Pair with a given sum "+x+" is ("+A[i]+" + "+A[j]+")");
}
}
}
}
OUTPUT:
Enter the number :
3
Pair with a given sum 3 is (1+2)
Comments
Post a Comment
Please do not enter any spam link in the comment box.