# Print pairs with given sum

Problem: For a given array of positive or negative integers, find all pairs possible with given input sum.

Problem Explanation: For a given array a ={4, -3, 8, 1, 6, 2} and desired sum 10, there are only two pairs {6, 4} and {2, 8}.

Java Implementation

```   public void pairCountForInputSum(int arr[], int requiredSum) {

Map<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
int count = 0;
for (int i = 0; i < arr.length; i++) {

if (hashMap.get(arr[i]) != null) {
count++;
System.out.println("{" + arr[i] + "," + hashMap.get(arr[i]) + "}");
} else {
hashMap.put(requiredSum - arr[i], arr[i]);
}

}

System.out.println("Total pairs: " + count);
}
```

TimeComplexity: O(n)

Note: If you find any other better way to approach to this problem or you find any issue/error in above code snippets/approaches – please share it in the comments section below and get a chance to enrol free of cost for an online Live Data Structure and Algorithm course (specially designed for interview preparation for software companies like Amazon, Google, Facebook, FlipKart, SnapDeal, HealthKart…)