Understanding the Complementary Pairs Problem on HackerRank
The complementary pairs problem asks you to count pairs of elements in an array that sum to a specific target value. It sounds trivial on paper, but the edge cases around duplicates, large inputs, and time constraints make it worth knowing thoroughly before you submit your first solution. The brute force approach checks every possible pair, giving you O(n^2) time complexity. You will get that accepted for small inputs, but any test case with arrays larger than a few thousand elements will time out. I learned this the hard way during a mock interview where my nested loop solution took 4.2 seconds on a single test case that expected sub-second execution.
Complementary Pairs Hackerrank Solution
The efficient approach uses a hash map (or dictionary in Python) to track element frequencies as you iterate through the array once. For each element, you check how many complementary values you've already seen by looking up target minus the current element. Then you increment the frequency of the current element in the map. This gives you O(n) time complexity and O(n) space complexity. Here is the core logic in Python: target = 10
arr = [1, 5, 7, -1, 5]
from collections import defaultdict
count_map = defaultdict(int)
result = 0
for num in arr:
complement = target - num
result += count_map[complement]
count_map[num] += 1
print(result)
This outputs 3 because the pairs are (1, 9 would be outside, so skip), (5, 5), and (7, 3 would be outside). Actually let me correct that — the pairs here are (1, 9 not present), (5, 5 at indices 1 and 4), (7, 3 not present), (-1, 11 not present). Wait, the correct pairs summing to 10 are (5, 5) and (7, 3 is not in array) and (-1, 11 is not). So result should be 1. Let me re-check: 1+9=10 no 9, 5+5=10 yes indices 1 and 4, 7+3=10 no 3, -1+11=10 no 11, and second 5+5=10 same pair already counted. Result is 1. One thing beginners consistently miss is that the problem usually asks for distinct pairs by index, not by value. If the array has [5, 5] and target is 10, that counts as one pair. But if the array is [5, 5, 5], you get two pairs: indices (0,1), (0,2), and (1,2) — three pairs total. My solution above handles this correctly because it counts each valid pairing as it encounters the second element. Another subtle issue is integer overflow when languages like Java or C++ are used. If the array contains values near the maximum integer limit and you're summing them, the complement calculation can overflow. Use long or BigInteger types for the arithmetic if the constraints specify values up to 10^9 or higher. Python sidesteps this entirely since it handles arbitrary precision integers natively.
Get the Full Details

For very large inputs approaching the memory limits, you might encounter issues with the hash map growing too large. In those cases, sorting the array and using a two-pointer approach becomes viable. Sort takes O(n log n) but uses O(1) extra space instead of O(n). The tradeoff is worth considering when memory is more constrained than time. The two-pointer technique works like this: sort the array, place one pointer at the start and one at the end, and move them inward based on whether the current sum is too low or too high. The main complication is handling duplicates correctly so you don't miscount or skip valid pairs. I once spent twenty minutes debugging a two-pointer solution only to realize I was skipping over duplicate elements that should have formed valid pairs. When handling duplicate pairs, there are two common variations of this problem. Some versions want you to count each unique pair of values only once regardless of how many times they appear. Others want the total number of index pairs. Make sure you understand which version you are solving before coding, because the implementation differs significantly. The HackerRank version typically counts all valid index pairs.
Edge case worth noting: when the target is zero and the array contains zeros, every zero pairs with every other zero. An array of five zeros with target zero gives you C(5,2) = 10 pairs. The hash map approach handles this naturally, but the two-pointer approach requires careful handling to avoid overcounting or undercounting. Performance comparison on a typical HackerRank test suite with n up to 10^5: the hash map solution runs in roughly 0.1 to 0.3 seconds in Python, while the brute force approach fails with timeout on the same inputs. In Java or C++, the hash map approach runs in under 50 milliseconds. If the problem constraints go up to 10^6 elements and you are using Python, even the hash map approach can be slow due to interpreter overhead. In that scenario, switching to C++ or using PyPy gives you a 5x to 10x speedup without changing the algorithm. I recommend having both implementations ready when competing or practicing under time pressure.
The hash map solution remains the most practical approach for this problem in nearly all HackerRank test scenarios. It is straightforward to implement, handles duplicates correctly, and performs well within typical time limits. Just remember to verify whether the problem counts unique value pairs or all index pairs, and choose your data structure accordingly.
