Given an unsorted array of integers, find a pair with a 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 the given array and return if the desired sum is found. This is demonstrated below in C, Java and Python:

C #include <stdio.h> // Naive method to find a pair in an array with given sum void findPair(int arr[], int n, int sum) { // consider each element except last element for (int i = 0; i < n - 1; i++) { // start from i'th element till last element for (int j = i + 1; j < n; j++) { // if desired sum is found, print it and return if (arr[i] + arr[j] == sum) { printf("Pair found at index %d and %d", i, j); return; } } } // No pair with given sum exists in the array printf("Pair not found"); } // Find pair with given sum in the array int main() { int arr[] = { 8, 7, 2, 5, 3, 1 }; int sum = 10; int n = sizeof(arr)/sizeof(arr[0]); findPair(arr, n, sum); return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include <stdio.h> // Naive method to find a pair in an array with given sum void findPair ( int arr [ ] , int n , int sum ) { // consider each element except last element for ( int i = 0 ; i < n - 1 ; i ++ ) { // start from i'th element till last element for ( int j = i + 1 ; j < n ; j ++ ) { // if desired sum is found, print it and return if ( arr [ i ] + arr [ j ] == sum ) { printf ( "Pair found at index %d and %d" , i , j ) ; return ; } } } // No pair with given sum exists in the array printf ( "Pair not found" ) ; } // Find pair with given sum in the array int main ( ) { int arr [ ] = { 8 , 7 , 2 , 5 , 3 , 1 } ; int sum = 10 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findPair ( arr , n , sum ) ; return 0 ; } Download Run Code Output:



Pair found at index 0 and 2

Java class Main { // Naive method to find a pair in an array with given sum public static void findPair(int[] A, int sum) { // consider each element except last element for (int i = 0; i < A.length - 1; i++) { // start from i'th element till last element for (int j = i + 1; j < A.length; j++) { // if desired sum is found, print it and return if (A[i] + A[j] == sum) { System.out.println("Pair found at index " + i + " and " + j); return; } } } // No pair with given sum exists in the array System.out.println("Pair not found"); } public static void main (String[] args) { int[] A = { 8, 7, 2, 5, 3, 1 }; int sum = 10; findPair(A, sum); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 class Main { // Naive method to find a pair in an array with given sum public static void findPair ( int [ ] A , int sum ) { // consider each element except last element for ( int i = 0 ; i < A . length - 1 ; i ++ ) { // start from i'th element till last element for ( int j = i + 1 ; j < A . length ; j ++ ) { // if desired sum is found, print it and return if ( A [ i ] + A [ j ] == sum ) { System . out . println ( "Pair found at index " + i + " and " + j ) ; return ; } } } // No pair with given sum exists in the array System . out . println ( "Pair not found" ) ; } public static void main ( String [ ] args ) { int [ ] A = { 8 , 7 , 2 , 5 , 3 , 1 } ; int sum = 10 ; findPair ( A , sum ) ; } } Download Run Code Output:



Pair found at index 0 and 2

Python # Naive method to find a pair in a list with given sum def findPair(A, sum): # consider each element except last element for i in range(len(A) - 1): # start from i'th element till last element for j in range(i + 1, len(A)): # if desired sum is found, print it and return if A[i] + A[j] == sum: print("Pair found at index", i, "and", j) return # No pair with given sum exists in the list print("Pair not found") if __name__ == '__main__': A = [8, 7, 2, 5, 3, 1] sum = 10 findPair(A, sum) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 # Naive method to find a pair in a list with given sum def findPair ( A , sum ) : # consider each element except last element for i in range ( len ( A ) - 1 ) : # start from i'th element till last element for j in range ( i + 1 , len ( A ) ) : # if desired sum is found, print it and return if A [ i ] + A [ j ] == sum : print ( "Pair found at index" , i , "and" , j ) return # No pair with given sum exists in the list print ( "Pair not found" ) if __name__ == '__main__' : A = [ 8 , 7 , 2 , 5 , 3 , 1 ] sum = 10 findPair ( A , sum ) 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(nlog(n)) 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 the 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. Below is C++, Java and Python implementation based on the idea –

C++ #include <iostream> #include <algorithm> // Function to find a pair in an array with given sum using Sorting void findPair(int arr[], int n, int sum) { // sort the array in ascending order std::sort(arr, arr + n); // maintain two indices pointing to end-points of the array int low = 0; int high = n - 1; // reduce search space arr[low..high] at each iteration of the loop // loop till low is less than high while (low < high) { // sum found if (arr[low] + arr[high] == sum) { std::cout << "Pair found"; return; } // increment low index if total is less than the desired sum // decrement high index is total is more than the sum (arr[low] + arr[high] < sum)? low++: high--; } // No pair with given sum exists in the array std::cout << "Pair not found"; } // Find pair with given sum in the array int main() { int arr[] = { 8, 7, 2, 5, 3, 1}; int sum = 10; int n = sizeof(arr)/sizeof(arr[0]); findPair(arr, n, sum); return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 #include <iostream> #include <algorithm> // Function to find a pair in an array with given sum using Sorting void findPair ( int arr [ ] , int n , int sum ) { // sort the array in ascending order std :: sort ( arr , arr + n ) ; // maintain two indices pointing to end-points of the array int low = 0 ; int high = n - 1 ; // reduce search space arr[low..high] at each iteration of the loop // loop till low is less than high while ( low < high ) { // sum found if ( arr [ low ] + arr [ high ] == sum ) { std :: cout << "Pair found" ; return ; } // increment low index if total is less than the desired sum // decrement high index is total is more than the sum ( arr [ low ] + arr [ high ] < sum ) ? low ++ : high -- ; } // No pair with given sum exists in the array std :: cout << "Pair not found" ; } // Find pair with given sum in the array int main ( ) { int arr [ ] = { 8 , 7 , 2 , 5 , 3 , 1 } ; int sum = 10 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findPair ( arr , n , sum ) ; return 0 ; } Download Run Code Output:



Pair found

Java import java.util.Arrays; class Main { // Naive method to find a pair in an array with given sum public static void findPair(int[] A, int sum) { // sort the array in ascending order Arrays.sort(A); // maintain two indices pointing to end-points of the array int low = 0; int high = A.length - 1; // reduce search space arr[low..high] at each iteration of the loop // loop till low is less than high while (low < high) { // sum found if (A[low] + A[high] == sum) { System.out.println("Pair found"); return; } // increment low index if total is less than the desired sum // decrement high index is total is more than the sum if (A[low] + A[high] < sum) { low++; } else{ high--; } } // No pair with given sum exists in the array System.out.println("Pair not found"); } // Find pair with given sum in the array public static void main (String[] args) { int[] A = { 8, 7, 2, 5, 3, 1 }; int sum = 10; findPair(A, sum); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 import java . util . Arrays ; class Main { // Naive method to find a pair in an array with given sum public static void findPair ( int [ ] A , int sum ) { // sort the array in ascending order Arrays . sort ( A ) ; // maintain two indices pointing to end-points of the array int low = 0 ; int high = A . length - 1 ; // reduce search space arr[low..high] at each iteration of the loop // loop till low is less than high while ( low < high ) { // sum found if ( A [ low ] + A [ high ] == sum ) { System . out . println ( "Pair found" ) ; return ; } // increment low index if total is less than the desired sum // decrement high index is total is more than the sum if ( A [ low ] + A [ high ] < sum ) { low ++ ; } else { high -- ; } } // No pair with given sum exists in the array System . out . println ( "Pair not found" ) ; } // Find pair with given sum in the array public static void main ( String [ ] args ) { int [ ] A = { 8 , 7 , 2 , 5 , 3 , 1 } ; int sum = 10 ; findPair ( A , sum ) ; } } Download Run Code Output:



Pair found

Python # Naive method to find a pair in a list with given sum def findPair(A, sum): # sort the list in ascending order A.sort() # maintain two indices pointing to end-points of the list (low, high) = (0, len(A) - 1) # reduce search space A[low..high] at each iteration of the loop # loop till low is less than high while low < high: if A[low] + A[high] == sum: # sum found print("Pair found") return # increment low index if total is less than the desired sum # decrement high index is total is more than the sum if A[low] + A[high] < sum: low = low + 1 else: high = high - 1 # No pair with given sum exists in the list print("Pair not found") # Find pair with given sum in the list if __name__ == '__main__': A = [8, 7, 2, 5, 3, 1] sum = 10 findPair(A, sum) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 # Naive method to find a pair in a list with given sum def findPair ( A , sum ) : # sort the list in ascending order A . sort ( ) # maintain two indices pointing to end-points of the list ( low , high ) = ( 0 , len ( A ) - 1 ) # reduce search space A[low..high] at each iteration of the loop # loop till low is less than high while low < high : if A [ low ] + A [ high ] == sum : # sum found print ( "Pair found" ) return # increment low index if total is less than the desired sum # decrement high index is total is more than the sum if A [ low ] + A [ high ] < sum : low = low + 1 else : high = high - 1 # No pair with given sum exists in the list print ( "Pair not found" ) # Find pair with given sum in the list if __name__ == '__main__' : A = [ 8 , 7 , 2 , 5 , 3 , 1 ] sum = 10 findPair ( A , sum ) Download Run Code Output:



Pair found







The time complexity of above solution is O(nlog(n)) 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 check if the 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. The algorithm can be implemented as follows in C++, Java and Python:

C++ #include <iostream> #include <unordered_map> // Function to find a pair in an array with given sum using Hashing void findPair(int arr[], int n, int sum) { // create an empty map std::unordered_map<int, int> map; // do for each element for (int i = 0; i < n; i++) { // check if pair (arr[i], sum-arr[i]) exists // if difference is seen before, print the pair if (map.find(sum - arr[i]) != map.end()) { std::cout << "Pair found at index " << map[sum - arr[i]] << " and " << i; return; } // store index of current element in the map map[arr[i]] = i; } // we reach here if pair is not found std::cout << "Pair not found"; } // Find pair with given sum in the array int main() { int arr[] = { 8, 7, 2, 5, 3, 1}; int sum = 10; int n = sizeof(arr)/sizeof(arr[0]); findPair(arr, n, sum); return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 #include <iostream> #include <unordered_map> // Function to find a pair in an array with given sum using Hashing void findPair ( int arr [ ] , int n , int sum ) { // create an empty map std :: unordered_map < int , int > map ; // do for each element for ( int i = 0 ; i < n ; i ++ ) { // check if pair (arr[i], sum-arr[i]) exists // if difference is seen before, print the pair if ( map . find ( sum - arr [ i ] ) != map . end ( ) ) { std :: cout << "Pair found at index " << map [ sum - arr [ i ] ] << " and " << i ; return ; } // store index of current element in the map map [ arr [ i ] ] = i ; } // we reach here if pair is not found std :: cout << "Pair not found" ; } // Find pair with given sum in the array int main ( ) { int arr [ ] = { 8 , 7 , 2 , 5 , 3 , 1 } ; int sum = 10 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findPair ( arr , n , sum ) ; return 0 ; } Download Run Code Output:



Pair found at index 0 and 2

Java import java.util.HashMap; import java.util.Map; class Main { // Naive method to find a pair in an array with given sum public static void findPair(int[] A, int sum) { // create an empty Hash Map Map<Integer, Integer> map = new HashMap<>(); // do for each element for (int i = 0; i < A.length; i++) { // check if pair (A[i], sum-A[i]) exists // if difference is seen before, print the pair if (map.containsKey(sum - A[i])) { System.out.println("Pair found at index " + map.get(sum - A[i]) + " and " + i); return; } // store index of current element in the map map.put(A[i], i); } // No pair with given sum exists in the array System.out.println("Pair not found"); } // Find pair with given sum in the array public static void main (String[] args) { int[] A = { 8, 7, 2, 5, 3, 1 }; int sum = 10; findPair(A, sum); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 import java . util . HashMap ; import java . util . Map ; class Main { // Naive method to find a pair in an array with given sum public static void findPair ( int [ ] A , int sum ) { // create an empty Hash Map Map < Integer , Integer > map = new HashMap <> ( ) ; // do for each element for ( int i = 0 ; i < A . length ; i ++ ) { // check if pair (A[i], sum-A[i]) exists // if difference is seen before, print the pair if ( map . containsKey ( sum - A [ i ] ) ) { System . out . println ( "Pair found at index " + map . get ( sum - A [ i ] ) + " and " + i ) ; return ; } // store index of current element in the map map . put ( A [ i ] , i ) ; } // No pair with given sum exists in the array System . out . println ( "Pair not found" ) ; } // Find pair with given sum in the array public static void main ( String [ ] args ) { int [ ] A = { 8 , 7 , 2 , 5 , 3 , 1 } ; int sum = 10 ; findPair ( A , sum ) ; } } Download Run Code Output:



Pair found at index 0 and 2

Python # Naive method to find a pair in a list with given sum def findPair(A, sum): # create an empty Hash Map dict = {} # do for each element for i, e in enumerate(A): # check if pair (e, sum-e) exists # if difference is seen before, print the pair if sum - e in dict: print("Pair found at index", dict.get(sum - e), "and", i) return # store index of current element in the dictionary dict[e] = i # No pair with given sum exists in the list print("Pair not found") # Find pair with given sum in the list if __name__ == '__main__': A = [8, 7, 2, 5, 3, 1] sum = 10 findPair(A, sum) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 # Naive method to find a pair in a list with given sum def findPair ( A , sum ) : # create an empty Hash Map dict = { } # do for each element for i , e in enumerate ( A ) : # check if pair (e, sum-e) exists # if difference is seen before, print the pair if sum - e in dict : print ( "Pair found at index" , dict . get ( sum - e ) , "and" , i ) return # store index of current element in the dictionary dict [ e ] = i # No pair with given sum exists in the list print ( "Pair not found" ) # Find pair with given sum in the list if __name__ == '__main__' : A = [ 8 , 7 , 2 , 5 , 3 , 1 ] sum = 10 findPair ( A , sum ) 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: Extend the solution to print all pairs in the array having given sum.











(103 votes, average: 4.92 out of 5)

Loading...

Thanks for reading.

