Java ProgramsArraysFind Pair With Given Sum

Find Pair With Given Sum in Java

intermediate·  Arrays  ·  Array

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.

Input
[6, 4, 12, 10, 22, 8], target = 18
Output
Pair: (6, 12)

Java Program

Java
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

Pair: (6, 12)

Core Logic

Trying every possible pair of elements, and stopping as soon as one adds up to the target, checks every combination directly.

How It Works
  1. 1The outer loop picks a first element, arr[i].
  2. 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. 3arr[i] + arr[j] == target checks whether the current pair adds up to the target.
  4. 4The first matching pair found is printed, and both loops exit immediately.
For [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.

Complexity
Time Complexity: O(n²)Space Complexity: O(1)

Why: Every pair of indices is checked directly, so the nested loops do up to n² comparisons with no extra memory.

Key Concepts

nested for loopearly exit with break

Approach 2: HashSet

Java
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

Pair: (6, 12)

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.

How It Works
  1. 1A HashSet<Integer> named seen starts empty.
  2. 2For each number, complement = target - num computes the value that would complete a pair summing to the target.
  3. 3seen.contains(complement) checks whether that complement has already been encountered earlier in the scan.
  4. 4A match prints the pair immediately; otherwise, the current number is added to seen before moving on.
Scanning [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.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

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.

Key Concepts

HashSetcomplement lookup

Related Programs