find pair of numbers in array that add to given sum
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Finding a pair of numbers in an array that sums to a given number is a classic problem in computer science and programming. This problem can be encountered in numerous applications such as financial calculations, game mechanics, and data analytics. The task is to identify two distinct numbers within an array that add up to a specific target value. This article will explore multiple approaches to solving this problem, ranging from brute force to optimized algorithms.
Problem Statement
Given an array of integers and a target sum, find all pairs of numbers that add up to the target sum. Assume each input would have exactly one solution, and the same element cannot be used twice. Note that the pair should be counted only once, not in both orders.
Approaches
1. Brute Force Approach
Description
The simplest approach is to use two nested loops to check every possible pair of numbers in the array to determine if their sum matches the target.
Algorithm
- Initialize two loops: an outer loop to pick the first element and an inner loop to pick the second.
- For each pair of elements, check if their sum matches the target sum.
- Return the pair when a match is found.
Implementation (Python)
- If it exists, return the pair.
- Otherwise, add the current element to the hash map.
- Calculate the sum of the elements at both pointers.
- If the sum matches the target, return the pair.
- If the sum is less than the target, increment the left pointer.
- If the sum is greater than the target, decrement the right pointer.
- Multiple pairs: Modify the approach to return all unique pairs that add up to the target sum. This is slightly more complicated and involves storing pairs already found and ensuring they're unique.
- Closest Sum: Instead of finding a precise target sum, find pairs whose sum is closest to a given number. This often requires additional logic during the two-pointer method.
Related reading
- Find pairs in an array such that ab k , where k is a given integer
- find path cross matrix with max sum forward then backward
- Find rank of a decimal number based on function F N rank
- Find rectangle with the largest sum of integers that lies on its border in C
- Find rectangles that contain point – Efficient Algorithm
- Find running median from a stream of integers
- Find Second largest number in array at most nlog₂n−2 comparisons
- Find set of numbers in one collection that adds up to a number in another

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.