Given Matrix, the following program shows how to extract all rows which have a pair such that their sum is equal to a specific number, here denoted as K.
Input : test_list = [[1, 5, 3, 6], [4, 3, 2, 1], [7, 2, 4, 5], [6, 9, 3, 2]], k = 8
Output : [[1, 5, 3, 6], [6, 9, 3, 2]]
Explanation : 5 + 3 = 8 and 6 + 2 = 8.
Input : test_list = [[1, 5, 4, 6], [4, 3, 2, 1], [7, 2, 4, 5], [6, 9, 3, 7]], k = 8
Output : []
Explanation : No list with 8 as pair summation.
Method 1: Using loop and list comprehension
In this we perform the task of getting all the pair whose sum is equal to a given value using external function and filtering of lists is done using list comprehension.
Python3
def pair_sum(x, k):
for idx in range ( len (x)):
for ix in range (idx + 1 , len (x)):
if x[idx] + x[ix] = = k:
return True
return False
test_list = [[ 1 , 5 , 3 , 6 ], [ 4 , 3 , 2 , 1 ], [ 7 , 2 , 4 , 5 ], [ 6 , 9 , 3 , 2 ]]
print ( "The original list is : " + str (test_list))
k = 8
res = [ele for ele in test_list if pair_sum(ele, k)]
print ( "Filtered Rows : " + str (res))
|
OutputThe original list is : [[1, 5, 3, 6], [4, 3, 2, 1], [7, 2, 4, 5], [6, 9, 3, 2]]
Filtered Rows : [[1, 5, 3, 6], [6, 9, 3, 2]]
Time Complexity: O(n*m)
Auxiliary Space: O(k)
Method 2 : Using filter() and lambda
In this, we perform the task of filtering using filter() and lambda function.
Python3
def pair_sum(x, k):
for idx in range ( len (x)):
for ix in range (idx + 1 , len (x)):
if x[idx] + x[ix] = = k:
return True
return False
test_list = [[ 1 , 5 , 3 , 6 ], [ 4 , 3 , 2 , 1 ], [ 7 , 2 , 4 , 5 ], [ 6 , 9 , 3 , 2 ]]
print ( "The original list is : " + str (test_list))
k = 8
res = list ( filter ( lambda ele: pair_sum(ele, k), test_list))
print ( "Filtered Rows : " + str (res))
|
Output:
The original list is : [[1, 5, 3, 6], [4, 3, 2, 1], [7, 2, 4, 5], [6, 9, 3, 2]]
Filtered Rows : [[1, 5, 3, 6], [6, 9, 3, 2]]
Time Complexity: O(m*n) where m and n is the number of rows and columns in the list
Auxiliary Space: O(k) additional space of size k is created
Method #3:Using itertools.filterfalse() method
Python3
import itertools
def pair_sum(x, k):
for idx in range ( len (x)):
for ix in range (idx + 1 , len (x)):
if x[idx] + x[ix] = = k:
return True
return False
test_list = [[ 1 , 5 , 3 , 6 ], [ 4 , 3 , 2 , 1 ], [ 7 , 2 , 4 , 5 ], [ 6 , 9 , 3 , 2 ]]
print ( "The original list is : " + str (test_list))
k = 8
res = list (itertools.filterfalse( lambda ele: not pair_sum(ele, k), test_list))
print ( "Filtered Rows : " + str (res))
|
OutputThe original list is : [[1, 5, 3, 6], [4, 3, 2, 1], [7, 2, 4, 5], [6, 9, 3, 2]]
Filtered Rows : [[1, 5, 3, 6], [6, 9, 3, 2]]
Time Complexity: O(N*N)
Auxiliary Space: O(N*N)
Method #4: Using sets
Use sets to solve this problem. We can create an empty set called seen and iterate through the list of numbers. For each number num, we check if k – num is in seen. If it is, then we have found a pair that adds up to k. If it is not, then we add num to seen. If we iterate through the entire list and do not find a pair, then we return False.
Approach:
- Define the function pair_sum(x, k) that takes in a list x and an integer k.
- Create an empty set called seen.
- Iterate through the list of numbers x.
- For each number num, check if k – num is in seen. If it is, then return True as we have found a pair that adds up to k.
- If k – num is not in seen, then add num to seen.
- If we iterate through the entire list and do not find a pair, then return False.
- Call the pair_sum function with the test_list and k variables.
- Print the result.
Python3
def pair_sum(x, k):
seen = set ()
for num in x:
if k - num in seen:
return True
seen.add(num)
return False
test_list = [[ 1 , 5 , 3 , 6 ], [ 4 , 3 , 2 , 1 ], [ 7 , 2 , 4 , 5 ], [ 6 , 9 , 3 , 2 ]]
print ( "The original list is : " + str (test_list))
k = 8
res = [ele for ele in test_list if pair_sum(ele, k)]
print ( "Filtered Rows : " + str (res))
|
OutputThe original list is : [[1, 5, 3, 6], [4, 3, 2, 1], [7, 2, 4, 5], [6, 9, 3, 2]]
Filtered Rows : [[1, 5, 3, 6], [6, 9, 3, 2]]
Time complexity: O(n^2) where n is the length of the longest list in test_list.
Auxiliary space: O(n) scenario where n is the length of the longest list in test_list, as we are storing each element in the set seen.
Method 5: Using the two-pointer technique.
Steps for this approach:
- Initialize two pointers left and right at the beginning and end of the list respectively.
- While left is less than right, do the following:
- Compute the sum of x[left] and x[right].
- If the sum is equal to k, return True.
- If the sum is less than k, increment left.
- If the sum is greater than k, decrement right.
- If the loop completes without finding a pair that sums to k, return False.
Python3
def pair_sum_using_two_pointer(x, k):
x_sorted = sorted (x)
left, right = 0 , len (x_sorted) - 1
while left < right:
curr_sum = x_sorted[left] + x_sorted[right]
if curr_sum = = k:
return True
elif curr_sum < k:
left + = 1
else :
right - = 1
return False
test_list = [[ 1 , 5 , 3 , 6 ], [ 4 , 3 , 2 , 1 ], [ 7 , 2 , 4 , 5 ], [ 6 , 9 , 3 , 2 ]]
print ( "The original list is : " + str (test_list))
k = 8
res = [ele for ele in test_list if pair_sum_using_two_pointer(ele, k)]
print ( "Filtered Rows : " + str (res))
|
OutputThe original list is : [[1, 5, 3, 6], [4, 3, 2, 1], [7, 2, 4, 5], [6, 9, 3, 2]]
Filtered Rows : [[1, 5, 3, 6], [6, 9, 3, 2]]
Time complexity: O(n*m(log m)) where n is the number of rows and m is the maximum number of columns in the rows (due to the sorting step in the two-pointer approach)
Auxiliary Space: O(n) (to store the filtered rows)