Find two pairs of pairs that sum to the same value
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 task is to find two distinct pairs of elements whose sums are equal. In algorithm terms, you want indices (i, j) and (k, l) such that a[i] + a[j] == a[k] + a[l], while also making sure the two pairs do not reuse the same array positions.
A Hash Map of Pair Sums Is the Standard Solution
The direct way to solve the problem is to examine every pair once, compute its sum, and remember the first pair seen for each sum. When another pair produces the same sum, compare indices. If the indices are disjoint, you found a valid answer.
A possible result is ((0, 6), (2, 4)), because 3 + 8 equals 7 + 2.
Why This Works
For every possible sum, the only information you need at first is one previously seen pair. When a second pair creates the same sum, you compare the four indices. If none overlap, the problem is solved. If they do overlap, you keep scanning.
The runtime is O(n^2) because there are n * (n - 1) / 2 pairs. That is unavoidable in the general case because the answer depends on relationships between pairs, not individual values.
Return Values Instead of Indices if You Prefer
Sometimes the problem asks for the values themselves rather than positions. You can map the returned indices back to the array.
Output:
Whether you return indices or values depends on whether duplicates matter. Indices are usually safer because they prove the pairs are distinct even when values repeat.
Handling Multiple Answers
Some inputs contain many valid pairs for the same sum. If you need all solutions rather than the first one, store a list of prior pairs for each sum instead of only one pair.
That increases memory use, but it is the right tradeoff if completeness matters.
Sorting Is Possible but Usually Less Natural
You could sort pair sums and then look for adjacent equal values, but that still requires generating all pairs first. In practice, the hash-map version is simpler and often faster to implement correctly.
The main thing to preserve is the non-overlapping-index rule. Many buggy solutions detect equal sums but accidentally reuse one element in both pairs.
Common Pitfalls
- Checking pair sums without verifying that the two pairs use four distinct indices.
- Returning values instead of indices in a way that hides duplicate-position reuse.
- Assuming the first repeated sum always gives a valid answer without overlap checks.
- Storing every pair unnecessarily when only one pair per sum is needed for the first-match problem.
- Forgetting that the general solution is naturally
O(n^2)because all pairs must be considered.
Summary
- Use a hash map from pair sum to previously seen pair indices.
- When the same sum appears again, verify that the two pairs do not share indices.
- Returning indices is safer than returning values when duplicates exist.
- Store all pairs per sum only if the problem asks for every solution.
- '
O(n^2)time is expected because the problem is defined over element pairs.'
Related reading
- Find two rectangles with minimum areas that cover all points
- Find whether two triangles intersect or not
- Finding 2 equal sum sub-sequences, with maximum sum?
- Finding a minimum bounding sphere for a frustum
- Finding a number that repeats even no of times where all the other numbers repeat odd no of times
- Finding a prime number after a given number
- Finding a ray that intersects a polygon as many times as possible
- Finding a stable placement of an irregular non-convex shape

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.