Java Program to Find Pairs with Given Sum
HashSet to check if the complement (sum - current element) exists; for example, use HashSet<Integer> set = new HashSet<>(); and check pairs while traversing.Examples
How to Think About It
Algorithm
Code
import java.util.HashSet; public class PairSum { public static void findPairs(int[] arr, int sum) { HashSet<Integer> set = new HashSet<>(); for (int num : arr) { int complement = sum - num; if (set.contains(complement)) { System.out.println("(" + complement + ", " + num + ")"); } set.add(num); } } public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; int sum = 5; findPairs(arr, sum); } }
Dry Run
Let's trace the example arr = [1, 2, 3, 4, 5] with sum = 5 through the code
Start with empty set
set = {}
Process first number 1
complement = 5 - 1 = 4; set does not contain 4; add 1 to set; set = {1}
Process second number 2
complement = 5 - 2 = 3; set does not contain 3; add 2 to set; set = {1, 2}
Process third number 3
complement = 5 - 3 = 2; set contains 2; print (2, 3); add 3 to set; set = {1, 2, 3}
Process fourth number 4
complement = 5 - 4 = 1; set contains 1; print (1, 4); add 4 to set; set = {1, 2, 3, 4}
Process fifth number 5
complement = 5 - 5 = 0; set does not contain 0; add 5 to set; set = {1, 2, 3, 4, 5}
| Number | Complement | Set Before | Pair Found | Set After |
|---|---|---|---|---|
| 1 | 4 | {} | No | {1} |
| 2 | 3 | {1} | No | {1, 2} |
| 3 | 2 | {1, 2} | Yes (2, 3) | {1, 2, 3} |
| 4 | 1 | {1, 2, 3} | Yes (1, 4) | {1, 2, 3, 4} |
| 5 | 0 | {1, 2, 3, 4} | No | {1, 2, 3, 4, 5} |
Why This Works
Step 1: Use a set to track seen numbers
The HashSet stores numbers we have seen so far, so we can quickly check if the complement exists.
Step 2: Calculate complement for each number
For each number, we find the number needed to reach the sum by subtracting the current number from the target sum.
Step 3: Print pairs when complement found
If the complement is already in the set, it means we found a pair that adds up to the sum, so we print it.
Alternative Approaches
public class PairSumBrute { public static void findPairs(int[] arr, int sum) { for (int i = 0; i < arr.length; i++) { for (int j = i + 1; j < arr.length; j++) { if (arr[i] + arr[j] == sum) { System.out.println("(" + arr[i] + ", " + arr[j] + ")"); } } } } public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; int sum = 5; findPairs(arr, sum); } }
import java.util.Arrays; public class PairSumTwoPointers { public static void findPairs(int[] arr, int sum) { Arrays.sort(arr); int left = 0, right = arr.length - 1; while (left < right) { int currentSum = arr[left] + arr[right]; if (currentSum == sum) { System.out.println("(" + arr[left] + ", " + arr[right] + ")"); left++; right--; } else if (currentSum < sum) { left++; } else { right--; } } } public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; int sum = 5; findPairs(arr, sum); } }
Complexity: O(n) time, O(n) space
Time Complexity
The program loops through the array once, and each lookup in the HashSet is O(1) on average, so total time is O(n).
Space Complexity
The HashSet stores up to n elements in the worst case, so space complexity is O(n).
Which Approach is Fastest?
The HashSet method is fastest for unsorted arrays with O(n) time, while sorting and two pointers take O(n log n). Brute force is slowest at O(n²).
| Approach | Time | Space | Best For |
|---|---|---|---|
| HashSet | O(n) | O(n) | Fastest for unsorted arrays |
| Sorting + Two Pointers | O(n log n) | O(1) | When sorting is acceptable |
| Brute Force | O(n²) | O(1) | Simple but slow, small arrays only |