Two pairs of numbers with same 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
The interesting version of "two pairs of numbers with the same sum" is usually an algorithm problem: given a list of numbers, can you find two distinct pairs whose sums are equal? A brute-force approach checks every pair against every other pair, but a better solution stores pair sums in a hash map. That reduces the problem to detecting when the same sum appears twice.
Restate the Problem Clearly
Given an array, find indices i, j, k, l such that:
- '
i < j' - '
k < l' - the pairs are distinct
- '
arr[i] + arr[j] = arr[k] + arr[l]'
Example:
- array:
[3, 4, 7, 1, 2, 9, 8] - pair
3 + 8 = 11 - pair
4 + 7 = 11
So the array contains two pairs with the same sum.
Brute Force Idea
The direct approach is:
- generate every pair
- compare each pair sum with every other pair sum
There are O(n^2) pairs, and comparing all of them against each other leads to O(n^4) work in the naive form.
That is usually much too slow once n grows.
Better Approach: Hash by Sum
A much better strategy is to compute each pair sum once and store the first pair that produced it.
If you later see the same sum again, you found a collision.
This runs in O(n^2) time because there are still O(n^2) pairs, but each pair sum is checked in constant expected hash time.
Distinct Indices Matter
Some versions of the problem require the two pairs to use four different elements. If so, you must check index overlap before accepting the result.
That distinction matters because some pair-sum collisions reuse one of the same elements.
Why the Hash Map Works
The core observation is simple: equal sums are collisions in the mapping from pairs to integers.
Instead of comparing all pairs against all others, the hash map groups pairs by their sum implicitly. The first time a sum appears, store it. The second time, you found the evidence you were looking for.
This is a classic pattern in algorithm design:
- compute a derived key
- store the earliest occurrence in a map
- detect repeats efficiently
Variations of the Problem
You may also be asked to:
- return all pair collisions, not just one
- return values instead of indices
- handle duplicates carefully
- count how many distinct equal-sum pair combinations exist
Those are all based on the same pair-sum idea, but the storage and duplicate-handling logic changes.
Space Complexity Tradeoff
The faster algorithm uses O(n^2) space in the worst case because many distinct pair sums may need to be stored.
That is usually acceptable for moderate n, but it matters if the input is large. As usual in algorithms, the speedup comes with a memory tradeoff.
Common Pitfalls
The biggest mistake is forgetting to define whether overlapping indices are allowed.
Another mistake is storing only values and not indices, which makes it harder to enforce pair distinctness correctly.
A third issue is assuming the O(n^2) time solution also uses small memory. It does not; the hash map can grow quadratically.
Summary
- The practical version of the problem is to detect two distinct pairs with the same sum in an array
- A hash map from sum to pair gives an
O(n^2)time solution - If the pairs must use four different elements, check index overlap explicitly
- The faster method trades memory for speed and can use
O(n^2)space - Clarify whether you need one example, all examples, indices, or values before implementing
Related reading
- Two Rectangles intersection
- Ultra symmetrical line algorithm?
- Unbiased random number generator using a biased one
- Understanding concept of Gaussian Mixture Models
- Understanding randomness
- Unfamiliar symbol in algorithm what does ∀ mean?
- Unique permutations with no mirrored or circular repetitions
- upper bound, lower bound

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.