Find Pair with given Sum in the Array

Given an unsorted array of integers, find a pair with given sum in it.

 

For example,


Input:
arr = [8, 7, 2, 5, 3, 1]
sum = 10

Output:
Pair found at index 0 and 2 (8 + 2)
OR
Pair found at index 1 and 4 (7 + 3)

 

1. Naive Approach –

 

Naive solution would be to consider every pair in given array and return if desired sum is found.

C

Download   Run Code

Java

Download   Run Code



Output:

Pair found at index 0 and 2

 
The time complexity of above solution is O(n2) and auxiliary space used by the program is O(1).

 

2. O(nlogn) solution using Sorting –

 

The idea is to sort the given array in ascending order and maintain search space by maintaining two indices (low and high) that initially points to two end-points of the array. Then we loop till low is less than high index and reduce search space arr[low..high] at each iteration of the loop. We compare sum of elements present at index low and high with desired sum and increment low if sum is less than the desired sum else we decrement high is sum is more than the desired sum. Finally, we return if pair found in the array.

C++

Download   Run Code

Java

Download   Run Code



Output:

Pair found

 
The time complexity of above solution is O(nlogn) and auxiliary space used by the program is O(1).
 

3. O(n) solution using Hashing –

 

We can use map to easily solve this problem in linear time. The idea is to insert each element of the array arr[i] in a map. We also checks if difference (arr[i], sum-arr[i]) already exists in the map or not. If the difference is seen before, we print the pair and return.

C++

Download   Run Code

Java

Download   Run Code



Output:

Pair found at index 0 and 2

 
The time complexity of above solution is O(n) and auxiliary space used by the program is O(n).

 
Exercise:

1. Print all pairs in the array having given sum.

2. Find a pair with the given sum in a BST

 
Thanks for reading.




Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 





Leave a Reply

Notify of
avatar
Sort by:   newest | oldest | most voted
arandomguy
Guest
arandomguy

I disabled adblock on this site. The ads on this site are not at all annoying, and this is the least I can do to support the team of techiedelight. You are doing a great job. Keep at it.

anonymous
Guest
anonymous

It’s worth noting that hash table operations are O(1) on average, but O(n) in the worst case. So in the worst case, the complexity of the hash based solution is O(n^2). In practice of course, if your hash function isn’t terrible, this is still probably the fastest solution of the three.

vitaly
Guest
vitaly

One note though, is that the Map solution does not take into account any duplicate values.

AllergicToBitches
Guest
AllergicToBitches

Awesome post..

glib
Guest
glib

Here are are the last two algorithms in C (using glib for the 3rd one): https://ideone.com/1PHYH3

suvro
Guest
suvro

isPairPresent : http://ideone.com/IyXytR

holod
Guest
holod

In all of the code samples here, you return after the first pair is found, without checking for any other pairs out there. This way you skip the second pair in a give input.

mepler
Guest
mepler

In the hashing solution, what is the purpose of storing the index of the current element in the Map (line 26 in Java)?

wpDiscuz