An algorithm to find a pair of sums from a list of numbers?
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
In the world of computer science and algorithm design, one common problem is finding pairs of numbers in a list that add up to a given sum. This is a quintessential problem that tests the foundational understanding of algorithmic efficiency and data structures. In this article, we will dive deep into one particular algorithm designed to solve this problem efficiently.
Problem Definition
Given a list of integers, nums
, and an integer target
, return indices of the two numbers such that they add up to the target.
Constraints
- Each input list will have exactly one solution.
- You may not use the same element twice.
- Return the answer in any order.
Algorithm
We'll review a commonly used algorithm that achieves this task with an optimal time complexity.
Approach: Hash
Map
The hash map (or dictionary in Python) offers an optimal solution to find the pair of sums. Here's how the algorithm works:
- Initialization: Create an empty hash map (dictionary) to store the elements of the list and their respective indices.
- Iterate through nums: For each element,
num:- Calculate the
complementastarget - num. - Check if the
complementexists in the hash map. - If it does, we found our two numbers, and we can return their indices.
- If not, store the current
numalong with its index in the hash map.
By leveraging the hash map, we achieve an average time complexity of due to the constant-time average complexity of hash table operations like insertions and lookups.
Example of the Approach
Consider the list nums = [2, 7, 11, 15]
and target = 9
. Here's how the algorithm proceeds:
- Initialize an empty hash map:
\{\} - Iteration 1:
num = 2complement = 9 - 2 = 7Hashmap:\{\}7is not in the hash map.- Insert
2into the hash map with index0:\{2: 0\}
- Iteration 2:
num = 7complement = 9 - 7 = 2Hashmap:\{2: 0\}2is in the hash map with index0.- Return
[0, 1].
The result is the indices of 2
and 7
, which add up to 9
.
Algorithm Implementation
Here is a Python implementation of the above algorithm:
- Space Complexity: O(n) — We use a hash map to store elements, which can hold up to
nelements in the worst-case scenario. - Time Complexity: O(n) — We traverse the list once, and each lookup in the hash map is, on average, .
Related reading
- An algorithm to find bounding box of closed bezier curves?
- An algorithm to find common edits
- An algorithm to find the nth largest number in two arrays of size n
- An algorithm to find the seed root of a given number
- An algorithm to solve a simple? array problem
- An efficient sorting algorithm for almost sorted list containing time data?
- An algorithm to increase / decrease load in an application based on the number of exceptions
- An algorithm to space out overlapping rectangles?

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 courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.