2 Sum All Pair II
Find all pairs of elements in a given array that sum to the pair the given target number. Return all the distinct pairs of values.
Assumptions
The given array is not null and has length of at least 2
The order of the values in the pair does not matter
Examples
A = {2, 1, 3, 2, 4, 3, 4, 2}, target = 6, return [[2, 4], [3, 3]]
Solution: Sort then left right pinch, or hashmap
Time Comp:
sort, then left right pinch: O(NlogN) sort + O(N) left to right = O(NlogN)
hashmap: O(N) traversal * O(1) map.containskey * O(1) map.put = *O(N)
Space Comp:
sort, then left right pinch: O(1)
hashMap: O(N) hashmap
Last updated
Was this helpful?