The problem of getting the number of pairs that lead to a particular solution has been dealt with many times, this article aims at extending that to 3 numbers and discussing several ways in which this particular problem can be solved. Let’s discuss certain ways in which this can be performed.
Method #1: Using Nested loops
This is the naive method in which this particular problem can be solved and takes the outer loop to iterate for each element and the inner loop checks for the remaining difference adding the pairs to the result.
Python3
# Python3 code to demonstrate # 3 element sum # using nested loops # initializing list test_list = [ 4 , 1 , 3 , 2 , 6 , 5 ] # initializing sum sum = 9 # printing original list print ( "The original list : " + str (test_list)) # using nested loops # 3 element sum res = [] for i in range ( 0 , len (test_list) - 2 ): for j in range (i + 1 , len (test_list) - 1 ): for k in range (j + 1 , len (test_list)): if test_list[i] + test_list[j] + test_list[k] = = sum : temp = [] temp.append(test_list[i]) temp.append(test_list[j]) temp.append(test_list[k]) res.append( tuple (temp)) # print result print ( "The 3 sum element list is : " + str (res)) |
The original list : [4, 1, 3, 2, 6, 5] The 3 sum element list is : [(4, 3, 2), (1, 3, 5), (1, 2, 6)]
Method #2 : Using itertools.combination()
This particular problem can also be done in a concise manner using the inbuilt function of a function. The combination function finds all the combinations taking K arguments leading to a particular sum.
Python3
# Python3 code to demonstrate # 3 element sum # using itertools.combination() from itertools import combinations # function to get the sum def test(val): return sum (val) = = 9 # initializing list test_list = [ 4 , 1 , 3 , 2 , 6 , 5 ] # initializing sum summation = 9 # printing original list print ( "The original list : " + str (test_list)) # using itertools.combination() # 3 element sum res = list ( filter (test, list (combinations(test_list, 3 )))) # print result print ( "The 3 sum element list is : " + str (res)) |
The original list : [4, 1, 3, 2, 6, 5] The 3 sum element list is : [(4, 3, 2), (1, 3, 5), (1, 2, 6)]
Time Complexity: O(n*n), where n is the length of the input list. This is because we’re using theitertools.combination() which has a time complexity of O(n*n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list
Method #3: Using itertools.combination() and additional check to improve runtime for 1000 items. also, it only checks if a match exists or not, if a match is found then output the combination.
Python3
import itertools def solve(nums, k): # rule out cases where no of elements are less than 3 if len (nums)< = 2 : return False else : # get all unique combinations permut = itertools.combinations(nums, 3 ) #rule out the loop if even the max * 3 < target max_value = max (nums) if max_value * 3 < k: return False else : #check until a match is found for i in permut: if i[ 0 ] + i[ 1 ] + i[ 2 ] = = k: return True return False nums = [ 4 , 1 , 3 , 1 ] k = 5 print ( "Test case :-\nk=" ,k, " \nnums = " ,nums) print ( "-----------------------------------------" ) Solution.solve(nums, k) print ( "-----------------------------------------" ) nums = [ 2 , 4 , 3 , 0 , 1 ] k = 0 print ( "Test case :-\nk=" ,k, " \nnums = " ,nums) print ( "-----------------------------------------" ) Solution.solve(nums, k) print ( "-----------------------------------------" ) nums = [ 0 ] k = 0 print ( "Test case :-\nk=" ,k, " \nnums = " ,nums) print ( "-----------------------------------------" ) Solution.solve(nums, k) print ( "-----------------------------------------" ) nums = [ 1 , 0 ] k = 1 print ( "Test case :-\nk=" ,k, " \nnums = " ,nums) print ( "-----------------------------------------" ) Solution.solve(nums, k) print ( "-----------------------------------------" ) nums = [ 2 , 0 , 1 ] k = 3 print ( "Test case :-\nk=" ,k, " \nnums = " ,nums) print ( "-----------------------------------------" ) Solution.solve(nums, k) print ( "-----------------------------------------" ) nums = [ 13 , 76 , 35 , 89 , 15 , 76 , 54 , 4 , 66 , 4 , 9 , 25 , 9 , 48 , 26 , 76 , 95 , 80 , 19 , 66 , 74 , 15 , 18 , 96 , 36 , 78 , 80 , 42 , 42 , 76 , 85 , 74 , 96 , 9 , 95 , 29 , 58 , 43 , 57 , 14 , 38 , 21 , 55 , 56 , 18 , 25 , 3 , 11 , 76 , 77 , 72 , 36 , 44 , 88 , 93 , 2 , 95 , 86 , 77 , 47 , 3 , 51 , 34 , 46 , 70 , 90 , 4 , 24 , 58 , 6 , 91 , 93 , 59 , 69 , 89 , 5 , 58 , 87 , 23 , 15 , 98 , 24 , 62 , 64 , 15 , 43 , 93 , 68 , 17 , 4 , 78 , 6 , 2 , 10 , 52 , 17 , 28 , 82 , 20 , 44 , 78 , 91 , 1 , 79 , 17 , 23 , 27 , 66 , 88 , 57 , 60 , 78 , 68 , 6 , 35 , 43 , 73 , 37 , 12 , 22 , 47 , 59 , 40 , 36 , 0 , 70 , 91 , 68 , 33 , 54 , 44 , 78 , 23 , 69 , 60 , 44 , 32 , 15 , 20 , 28 , 36 , 55 , 73 , 12 , 55 , 26 , 43 , 57 , 63 , 46 , 0 , 43 , 24 , 91 , 86 , 61 , 15 , 10 , 29 , 8 , 51 , 84 , 94 , 18 , 5 , 63 , 90 , 4 , 38 , 28 , 84 , 67 , 18 , 33 , 85 , 93 , 31 , 38 , 97 , 14 , 79 , 11 , 92 , 3 , 8 , 27 , 65 , 39 , 1 , 83 , 38 , 8 , 83 , 53 , 53 , 44 , 57 , 64 , 78 , 1 , 44 , 72 , 100 , 86 , 85 , 16 , 5 , 27 , 24 , 2 , 56 , 74 , 16 , 90 , 65 , 8 , 93 , 64 , 72 , 45 , 47 , 7 , 83 , 100 , 99 , 62 , 89 , 38 , 38 , 42 , 98 , 18 , 29 , 88 , 48 , 35 , 0 , 82 , 56 , 22 , 64 , 13 , 54 , 66 , 32 , 9 , 2 , 52 , 80 , 28 , 19 , 9 , 38 , 98 , 38 , 21 , 41 , 65 , 78 , 63 , 97 , 71 , 99 , 41 , 89 , 78 , 48 , 47 , 72 , 67 , 79 , 45 , 43 , 92 , 30 , 28 , 42 , 52 , 56 , 24 , 46 , 55 , 82 , 22 , 86 , 67 , 43 , 84 , 41 , 26 , 66 , 26 , 32 , 8 , 93 , 65 , 10 , 47 , 19 , 70 , 84 , 43 , 99 , 74 , 78 , 51 , 59 , 58 , 38 , 75 , 86 , 24 , 48 , 27 , 72 , 32 , 15 , 28 , 98 , 11 , 70 , 74 , 91 , 89 , 90 , 74 , 69 , 60 , 78 , 6 , 67 , 34 , 100 , 70 , 98 , 70 , 60 , 12 , 58 , 26 , 21 , 44 , 64 , 15 , 82 , 81 , 52 , 9 , 82 , 94 , 99 , 43 , 22 , 14 , 16 , 27 , 68 , 72 , 69 , 45 , 60 , 10 , 91 , 31 , 58 , 91 , 71 , 42 , 19 , 43 , 42 , 80 , 61 , 50 , 2 , 81 , 45 , 40 , 44 , 100 , 68 , 74 , 19 , 0 , 33 , 24 , 58 , 59 , 1 , 26 , 65 , 69 , 19 , 60 , 12 , 50 , 82 , 44 , 18 , 96 , 77 , 43 , 33 , 31 , 53 , 9 , 36 , 86 , 24 , 46 , 11 , 26 , 3 , 43 , 8 , 42 , 17 , 55 , 60 , 56 , 33 , 66 , 90 , 64 , 42 , 73 , 61 , 34 , 13 , 43 , 9 , 59 , 46 , 0 , 68 , 93 , 60 , 78 , 92 , 76 , 45 , 20 , 31 , 47 , 21 , 44 , 2 , 23 , 78 , 72 , 62 , 65 , 56 , 26 , 80 , 65 , 87 , 61 , 88 , 22 , 38 , 70 , 98 , 48 , 25 , 95 , 55 , 3 , 32 , 28 , 50 , 13 , 57 , 18 , 95 , 11 , 23 , 57 , 21 , 93 , 35 , 38 , 75 , 79 , 39 , 87 , 30 , 95 , 73 , 37 , 47 , 43 , 15 , 73 , 59 , 0 , 29 , 29 , 30 , 50 , 13 , 73 , 1 , 32 , 7 , 46 , 7 , 24 , 43 , 21 , 84 , 1 , 6 , 96 , 33 , 69 , 2 , 70 , 98 , 2 , 90 , 31 , 66 , 74 , 58 , 66 , 56 , 33 , 5 , 78 , 19 , 21 , 98 , 76 , 1 , 55 , 97 , 39 , 43 , 43 , 44 , 7 , 61 , 84 , 31 , 0 , 67 , 46 , 39 , 23 , 7 , 31 , 29 , 2 , 71 , 7 , 35 , 60 , 20 , 94 , 23 , 81 , 40 , 79 , 11 , 46 , 71 , 25 , 42 , 54 , 10 , 21 , 11 , 72 , 86 , 48 , 53 , 67 , 36 , 55 , 49 , 98 , 65 , 37 , 27 , 63 , 20 , 86 , 60 , 29 , 63 , 75 , 91 , 30 , 55 , 23 , 82 , 88 , 53 , 62 , 44 , 25 , 27 , 99 , 39 , 4 , 40 , 41 , 35 , 35 , 61 , 62 , 10 , 98 , 9 , 82 , 21 , 39 , 82 , 89 , 41 , 71 , 26 , 51 , 2 , 73 , 30 , 49 , 92 , 16 , 66 , 60 , 68 , 2 , 99 , 91 , 15 , 77 , 43 , 44 , 46 , 69 , 79 , 100 , 47 , 9 , 66 , 33 , 25 , 15 , 5 , 77 , 86 , 81 , 82 , 69 , 10 , 0 , 98 , 4 , 90 , 30 , 89 , 87 , 83 , 97 , 35 , 96 , 71 , 15 , 50 , 12 , 82 , 79 , 91 , 79 , 28 , 62 , 24 , 75 , 77 , 68 , 59 , 87 , 29 , 81 , 66 , 92 , 85 , 4 , 17 , 48 , 69 , 28 , 37 , 55 , 39 , 11 , 92 , 57 , 26 , 64 , 91 , 44 , 37 , 3 , 44 , 57 , 13 , 75 , 21 , 39 , 17 , 29 , 18 , 74 , 43 , 93 , 53 , 6 , 61 , 71 , 89 , 99 , 13 , 92 , 23 , 99 , 18 , 13 , 38 , 14 , 9 , 82 , 10 , 7 , 41 , 24 , 40 , 61 , 42 , 26 , 2 , 43 , 38 , 3 , 50 , 43 , 75 , 47 , 95 , 16 , 41 , 40 , 43 , 14 , 26 , 40 , 79 , 11 , 30 , 16 , 39 , 46 , 45 , 25 , 44 , 79 , 21 , 48 , 48 , 23 , 38 , 79 , 62 , 71 , 8 , 53 , 12 , 72 , 64 , 71 , 50 , 90 , 38 , 77 , 6 , 48 , 74 , 77 , 53 , 85 , 82 , 19 , 18 , 43 , 97 , 67 , 56 , 25 , 46 , 87 , 27 , 99 , 21 , 13 , 56 , 71 , 87 , 73 , 25 , 0 , 52 , 82 , 18 , 65 , 34 , 96 , 33 , 99 , 43 , 45 , 2 , 59 , 29 , 53 , 34 , 78 , 27 , 41 , 74 , 74 , 59 , 96 , 74 , 45 , 74 , 44 , 28 , 62 , 4 , 90 , 62 , 31 , 89 , 72 , 88 , 69 , 5 , 29 , 26 , 81 , 66 , 96 , 53 , 87 , 72 , 89 , 9 , 30 , 71 , 75 , 83 , 46 , 71 , 57 , 12 , 77 , 24 , 44 , 71 , 34 , 42 , 11 , 67 , 9 , 69 , 47 , 95 , 93 , 72 , 12 , 40 , 98 , 83 , 25 , 27 , 91 , 21 , 31 , 0 , 44 , 56 , 1 , 76 , 30 , 100 , 18 , 46 , 35 , 72 , 61 , 39 , 90 , 25 , 78 , 42 , 77 , 13 , 72 , 32 , 3 , 84 , 63 , 59 , 100 , 60 , 22 , 57 , 50 , 40 , 63 , 65 , 2 , 27 , 88 , 63 , 40 , 97 , 94 , 69 , 72 , 98 , 2 , 68 , 13 , 62 , 76 , 97 , 14 , 36 , 15 , 91 , 80 , 55 , 45 , 67 , 50 , 13 , 49 , 54 , 72 , 84 , 36 , 69 , 74 , 35 , 30 , 47 , 64 , 91 , 24 , 16 , 41 , 41 , 19 , 96 , 25 , 38 , 60 , 43 , 86 , 6 , 23 , 81 , 71 , 73 , 56 , 12 , 44 , 67 , 28 , 39 , 1 , 52 , 40 , 28 , 45 , 44 , 53 , 63 , 7 , 66 , 45 , 44 , 68 , 6 , 98 , 100 , 95 ] k = 787 print ( "Test case :-\nk=" ,k, " \nnums = " ,nums[ 0 : 10 ], "....." ) print ( "-----------------------------------------" ) Solution.solve(nums, k) print ( "-----------------------------------------" ) nums = [ 0 , 0 , 0 ] k = 0 print ( "Test case :-\nk=" ,k, " \nnums = " ,nums) print ( "-----------------------------------------" ) Solution.solve(nums, k) print ( "-----------------------------------------" ) |
Output :
Test case :- k= 5 nums = [4, 1, 3, 1] ----------------------------------------- Found match : (1, 3, 1) 5 ----------------------------------------- Test case :- k= 0 nums = [2, 4, 3, 0, 1] ----------------------------------------- Match not found. ----------------------------------------- Test case :- k= 0 nums = [0] ----------------------------------------- Match not found. ----------------------------------------- Test case :- k= 1 nums = [1, 0] ----------------------------------------- Match not found. ----------------------------------------- Test case :- k= 3 nums = [2, 0, 1] ----------------------------------------- Found match : (2, 0, 1) 3 ----------------------------------------- Test case :- k= 787 nums = [13, 76, 35, 89, 15, 76, 54, 4, 66, 4] ..... ----------------------------------------- Match not found. ----------------------------------------- Test case :- k= 0 nums = [0, 0, 0] ----------------------------------------- Found match : (0, 0, 0) 0 -----------------------------------------
Using numpy:
Algorithm:
- We define a function find_3_sum_elements which takes an input list arr and a target sum target as arguments.
- We initialize an empty list res to store the output tuples.
- We convert the input list into a numpy array using np.array(arr).
- We iterate over each pair of indices (i, j) in the input array using nested loops.
- For each pair (i, j), we compute the index k of the third element such that the sum of elements at indices (i, j, k) equals the target sum.
- If k is a valid index (i.e., it is not equal to i or j), we add the tuple (arr[i], arr[j], arr[k]) to the output list res.
- We return the final output list res.
Python3
import numpy as np def find_3_sum_elements(arr, target): res = [] arr = np.array(arr) for i in range ( len (arr)): for j in range (i + 1 , len (arr)): k = np.where(arr = = target - arr[i] - arr[j])[ 0 ] if len (k) > 0 and k[ 0 ] not in (i, j): res.append( tuple ( sorted ((arr[i], arr[j], arr[k[ 0 ]])))) return list ( set (res)) # Example usage arr = [ 4 , 1 , 3 , 2 , 6 , 5 ] target = 9 result = find_3_sum_elements(arr, target) print (result) |
Output:
[(1, 2, 6), (2, 3, 4), (1, 3, 5)]
Note that this algorithm has a time complexity of O(n^2) and an auxiliary space complexity of O(n) due to the use of the numpy array and the output list res.