Pair of Sum in an Array
Problem Statement
Problem Statement
Input will be an integer array and a sum value. We need to find whether there is a pair of integers present in the array whose sum is same as the input value.
Algorithm
Algorithm
Step 1. Define an empty set of integers.
Step 2. Loop through the array and check whether the element's complement (sum-element) present in the set. If present return true.
Step 3. If element's complement is not present in the set, add the element into the set.
Step 4. Return false
Java Code
Java Code
boolean existPairSum(int[] array,int sum){
Set<Integer> set = HashSet<Integer>();
for(int elt : array){
if(set.contains(sum-elt)){
return true;
}else{
set.add(i);
}
}
return false;
}