Finding difference target pair

In an unsorted array is there any algorithm by using it we can find the pair whose difference is given taget.

@gaurav19063 you can store all the elements in an unordered_map, and then, iterate over the array. Search for target - a[i] in the unordered_map in O(1) time, it is found, break, otherwise continue the search. The whole time complexity will be O(n)

is there two pointer approach possible like target sum.

gfg suggested the solution bu i still have doubt that this solution is wrong.

@gaurav19063 the approach i suggested is already working in O(n), even gfg also suggested using hashing only, for a more efficient solution. This gfg approach you share is using a brute force, nested loops technique that works in O(n^n) time

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.