Find A Pair With The Given Sum In An Array

Follow this tutorial and dive deep into the problem of finding pairs with a given sum in an array! 

The concept of array is quite wide and programmers often encounter problems related to it. From sorting an array to reverse an array to unsorted array to pair sum in array, you will face so many array related problems in your coding journey. 

Here, we are going to get into the details of finding the pair of sum in an array. We will look into the approaches to find the pairs present in the value of array. 

Be with us till the end of this blog and fill your information bank with the right knowledge and in-depth understanding. 

 

What are pair problems?

If you are preparing hard for the programming interviews, you should know that the problem of finding all pairs which may add up to the array value is quite popular in array coding questions. 

This question is also popularly known as two sum problem as you need to find out the pairs of any two elements in an array whose sum will be equal to the given number. 

For example; you are being given an array of integers whose values are [ 1, 2, 3, 4, 5, 6]. You need to find the pairs whose sum will be equal to 5. In this case, we will get two pairs i.e [ 1,4] , [ 2, 3] 

Hence, your program needs to print it all in a console. 

 

Though, in order to solve the question on pair sum in array, you need to scan through this question. Use the two pointes, one from left and other one from right so to scan your array. Also, consider if the array is sorted or not. If it is not sorted, you can sort it before scanning. 

If your array is sorted, the first element would be lowest and the last one would be the highest. If the sum of both the first and last elements would be equal to the given sum, both pointers will move towards each other so as to find other pairs. 

However, if it is less than the given sum, the second pointer will move towards the first pointer. If it is more than the sum of both the first or the last element, the first element will then move towards the second one to find the pair sum in array. 

 

 

Approaches To Find Pairs With The Given Sum In An Array 

To find pairs with a given sum in an array, you have to consider certain approaches. But for that you have to first look into a problem statement. 

You are given with the integer array of size n, determine if there exist at least two unique elements with the sum x . If there will be no such element we will print it as; there is no pair. 

 

Approach One  – Finding all the unique pairs 

To find all the unique pairs of elements, you can use the brute force approach. Here, you can consider all possible pairs in an array to check their sum. 

If there exists a pair with its sum equal to the x, we will print the pair and return it. If no pair exists, we will print there is no such pair. 

 

Take input array; 

0  1  2  3 4

10 5 18 12 9 

I    j 

10   5  18  12  9 

I            j 

10  5  18  12   9 

I                      j

10   5   8  12   9 

    I      j 

 

The two numbers are 5 +18 = 23 

Time complexity would be O[n] 

Space complexity would be O[1] 

 

Approach Two – Two Pointer Approach 

In this approach, we will first sort the given array. Then, we will use two pointers i.e left and right which can be initially pointed to the left and right of the array. 

Then we will follow the below mentioned steps until left will be less than right. The sum of all elements which are pointed by two pointers will be equal to x. Then we will print two elements to stop. 

If your sum will be less than the x, we can increment the left pointer as 1. If our sum will be greater than x, we will decrement the right pointer as 1. After accessing all elements , if there will be no pair, we will print as, there is no pair. 

In the two pointer approach, time complexity would be; O[nlogn] 

Space complexity in this case would be  O[1] 

 

Approach 3  – Using Hashing 

In the hashing technique, we will linearly access all elements in a given array. Say we are given with the input_array [i] which is an array element at any given stance. 

We will check it as X – input_array [i] which has been previously accessed from a given array. We will then print a pair and return it. If not, then we will move to the next element. 

After accessing all the elements in an array, if no such pair exists in the given sum, we will print it as there is no given pair. 

 

The input array in this case will be; 

0  1  2  3  4 

10  5  18  12  9 

10  5  18 12   9

      I 

10  5  18  12 9 

            I 

 

The two numbers will be 5 and 18 

Time complexity in this case would be; O [n] 

Space complexity in this case would be; O[n] 

 

Naive Solution 

The Naive approach here in this problem would be in order to loop all numbers and then loop it again in an array to look for the pair with the sum S. 

The running time regarding this solution would be O[n] even in the worst case we are looping through an array to find the pairs.

 

Wrapping Up 

To find a pair sum in an array, you can follow the above-mentioned approaches. Further, you can also look at the problem to reverse an array for better understanding of the concept. 

 

Also Read – Integration For WooCommerce QuickBooks