Find Pair With Given Sum in Java
Problem
A pair with a given sum is any two elements in the array whose values add up to a specific target number.
Given an array of integers and a target sum, find a pair of elements that add up to the target.
Java Program
public class PairWithSum {
public static void main(String[] args) {
int[] arr = {6, 4, 12, 10, 22, 8};
int target = 18;
boolean found = false;
for (int i = 0; i < arr.length && !found; i++) {
for (int j = i + 1; j < arr.length; j++) { // only pair with later elements
if (arr[i] + arr[j] == target) {
System.out.println("Pair: (" + arr[i] + ", " + arr[j] + ")");
found = true;
break;
}
}
}
}
}Output
Core Logic
Trying every possible pair of elements, and stopping as soon as one adds up to the target, checks every combination directly.
- 1The outer loop picks a first element,
arr[i]. - 2The inner loop, starting from
i + 1, pairs it with every element that comes after it — this avoids checking the same pair twice or pairing an element with itself. - 3
arr[i] + arr[j] == targetchecks whether the current pair adds up to the target. - 4The first matching pair found is printed, and both loops exit immediately.
[6, 4, 12, 10, 22, 8] with target = 18, the pair (6, 4) sums to 10, but the next pair (6, 12) sums to exactly 18, so it's reported.Key Point: Starting the inner loop at i + 1 rather than 0 is what keeps each pair of indices from being checked twice — (6, 12) and (12, 6) would otherwise both get tested separately for no reason.
Why: Every pair of indices is checked directly, so the nested loops do up to n² comparisons with no extra memory.
Key Concepts
Approach 2: HashSet
import java.util.HashSet;
import java.util.Set;
public class PairWithSumHashSet {
public static void main(String[] args) {
int[] arr = {6, 4, 12, 10, 22, 8};
int target = 18;
Set<Integer> seen = new HashSet<>();
for (int num : arr) {
int complement = target - num; // value needed to complete the pair
if (seen.contains(complement)) {
System.out.println("Pair: (" + complement + ", " + num + ")");
break;
}
seen.add(num);
}
}
}
Output
Core Logic
For each number, checking whether its complement — the value needed to reach the target — has already been seen, finds a matching pair in a single pass.
- 1A
HashSet<Integer>namedseenstarts empty. - 2For each number,
complement = target - numcomputes the value that would complete a pair summing to the target. - 3
seen.contains(complement)checks whether that complement has already been encountered earlier in the scan. - 4A match prints the pair immediately; otherwise, the current number is added to
seenbefore moving on.
[6, 4, 12, 10, 22, 8], 6 and 4 are added to seen first; when 12 is reached, its complement 18 - 12 = 6 is already in seen, so (6, 12) is reported.Key Point: This only ever looks backward at numbers already scanned, which is why the pair is reported as (complement, num) — the earlier number always comes first.
Why: Each number is checked against the set of previously seen numbers in constant time, at the cost of storing up to n numbers in the set.