Finding set of pairs that correspond to list of sums
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.
Introduction
In computational mathematics and computer science, one of the intriguing problems is finding sets of pairs that correspond to a given list of sums. This problem has applications in various fields, including cryptography, computer vision, and combinatorics. The challenge is to identify all possible pairs from a given list that result in a specified sum when added together.
Problem Definition
Given a list of numbers `L`, the task is to find all unique pairs of numbers `(a, b)` such that their sum is in a specified list of sums `S`. This problem can be broken down into several key components:
- Input List: A list `L` of integers.
- Target Sums: A list `S` of integers which are the target sums we are interested in.
- Output Pairs: A set of tuples `(a, b)` such that `a + b` is an element of `S`.
Approach
Brute Force Method
The simplest approach involves iterating through all possible pairs of the list to check if their sum exists in the set of target sums. Despite its simplicity, this method can be inefficient due to its time complexity, where is the size of the list `L`.
Algorithm:
- Initialize an empty list to store the pairs.
- Iterate over every element `a` in the list `L`.
- For each element `a`, iterate over every element `b` in the list `L`.
- Check if the sum `a + b` is in the list of sums `S`.
- If it is, add the pair `(a, b)` to the output list.
`Hash` Table Optimization
By utilizing a hash table (or dictionary in Python), we can optimize the search process, which reduces the average time complexity closer to , where is the number of sums.
Algorithm:
- Create a hash table that contains all sums as keys and their occurrence as values.
- Iterate over each number `a` in `L`.
- For each `a`, calculate the difference `d = s - a` for each `s` in `S`.
- Check if `d` is in the hash table:
- If present, store the pair `(a, d)`.
Example
Consider the list `L` = [1, 2, 3, 4, 5] and `S` = [5, 6, 7]. We need to find all pairs whose sums are either 5, 6, or 7.
Brute Force: Pairs: (1, 4), (2, 3), (2, 4), (1, 5), and (3, 4).
Hash Table Optimization:
- Start by creating a hash table for `L`.
- Attempt to pair each number with another that can result in a target sum from `S`.
- Efficiently retrieve possible pairs using the hash table.
Table Summary
| Aspect | Brute Force | Hash Table Optimization |
| Time Complexity | ||
| Space Complexity | ∗ | |
| Application Complexity | Simple to understand and implement Inefficient for large lists | Slightly complex due to hash tables More efficient |
* is the number of valid pairs found.
Challenges and Considerations
- Duplicate Elements: If `L` contains duplicate elements, care must be taken to avoid considering the same pair multiple times.
- Handling Negative Numbers: The algorithm seamlessly handles negative numbers, as their sums can be negative, zero, or positive sums as required.
- Edge Cases: Consider cases where `L` or `S` is empty, which should return an empty list of pairs.
Conclusion
Finding sets of pairs that correspond to a list of sums is a fundamental problem with multiple approaches, each with its trade-offs in complexity and efficiency. While brute force methods are simple and intuitive, they may be inadequate for larger datasets where optimized methods like using hash tables can provide significant performance improvements. Understanding these techniques is essential for tackling similar combinatorial problems in computer science and related applications.
Related reading
- Finding shortest repeating cycle in word?
- Finding smallest polygon covering a set of points in a grid
- Finding sorted sub-sequences in a permutation
- Finding square root without using sqrt function?
- Finding Strongly Connected Components in a graph through DFS
- Finding sum of elements in Swift array
- Finding subset with max/min set bits under XOR
- Finding Sum Of The Differences OF MAX and MIN of All Possible Subsets

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.