Find Pairs whose sum equal to a target sum
Filed under Deloitte
Free read 2 of 3Determine if index pairs or value pairs are required, and if duplicate pairs should be filtered out.
The question
Given an array of integers and a target sum, find all unique pairs of elements in the array that add up to the target sum.
Constraints
- Array length between 1 and 10^5.
- Elements can be negative, zero, or positive.
Examples
- Input
- arr = [2, 4, 3, 5, 7, 8, -1], target = 7
- Output
- [[2, 5], [4, 3], [8, -1]]
- Why
- These pairs sum up to the target 7.
Approach
Determine if index pairs or value pairs are required, and if duplicate pairs should be filtered out.
Check every possible pair using nested loops. Time: O(n^2), Space: O(1).
Use a hash set to track seen numbers. For each number, check if target - num exists in the set. Time: O(n), Space: O(n).
Empty arrays, arrays with fewer than two elements, or no valid pairs.
Tips
- What they are testing: Test with duplicate values in the array and targets with no matching pairs.
- Common mistake: Returning duplicate pairs when the same combination appears multiple times in the array.
Related Deloitte questions
Collected from public candidate posts and coding forums. Updated Nov 2026.