Given an unsorted integer array, find a pair with the given sum in it.

For example,

Input:
 
nums = [8, 7, 2, 5, 3, 1]
target = 10
 
Output:
 
Pair found (8, 2)
or
Pair found (7, 3)
 
 
Input:
 
nums = [5, 2, 6, 8, 1, 9]
target = 12
 
Output: Pair not found

Practice this problem

There are several methods to solve this problem using brute-force, sorting, and hashing. These are discussed below:

1. Using Brute-Force

A naive solution is to consider every pair in the given array and return if the desired sum is found. This approach is demonstrated below in C, Java, and Python:

C


Download  Run Code

Output:

Pair found (8, 2)

Java


Download  Run Code

Output:

Pair found (8, 2)

Python


Download  Run Code

Output:

Pair found (8, 2)

The time complexity of the above solution is O(n2) and doesn’t require any extra space, where n is the size of the input.

2. 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 endpoints of the array. Then reduce the search space nums[low…high] at each iteration of the loop by comparing the sum of elements present at indices low and high with the desired sum. Increment low if the sum is less than the expected sum; otherwise, decrement high if the sum is more than the desired sum. If the pair is found, return it.

Following is the C++, Java, and Python implementation based on the idea:

C++


Download  Run Code

Output:

Pair found (2, 8)

Java


Download  Run Code

Output:

Pair found (2, 8)

Python


Download  Run Code

Output:

Pair found (2, 8)

The time complexity of the above solution is O(n.log(n)) and doesn’t require any extra space.

3. Using Hashing

We can use a hash table to solve this problem in linear time. The idea is to insert each array element nums[i] into a map. We also check if difference (nums[i], target - nums[i]) already exists in the map or not. If the difference is seen before, print the pair and return. The algorithm can be implemented as follows in C++, Java, and Python:

C++


Download  Run Code

Output:

Pair found (8, 2)

Java


Download  Run Code

Output:

Pair found (8, 2)

Python


Download  Run Code

Output:

Pair found (8, 2)

The time complexity of the above solution is O(n) and requires O(n) extra space, where n is the size of the input.

 
Exercise: Extend the solution to print all pairs in the array having a given sum.

 
Related Posts:

Find a triplet with the given sum in an array

4–Sum Problem | Quadruplets with a given sum

Find a pair with the given sum in a BST