
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Find pairs with given sum such that elements of pair are in different rows in Python
Suppose we have a matrix of unique elements and a sum; we have to find all the pairs from the matrix whose sum is equal to given sum. Here, each element of pair will be taken from different rows.
So, if the input is like −
2 | 4 | 3 | 5 |
6 | 9 | 8 | 7 |
10 | 11 | 14 | 12 |
13 | 1 | 15 | 16 |
sum = 13, then the output will be [(2, 11), (4, 9), (3, 10), (5, 8), (12, 1)]
To solve this, we will follow these steps −
res := a new list
n := size of matrix
for i in range 0 to n, do
sort the list matrix[i]
for i in range 0 to n - 1, do
for j in range i + 1 to n, do
low := 0, high := n - 1
while low < n and high >= 0, do
if (matrix[i, low] + matrix[j, high]) is same as sum, then
pair := make pair using (matrix[i, low],matrix[j, high])
insert pair at the end of res
low := low + 1
high := high - 1
otherwise,
if (matrix[i][low] + matrix[j][high]) < sum, then
low := low + 1
otherwise,
high := high - 1
return res
Example (Python)
Let us see the following implementation to get better understanding −
MAX = 100 def sum_pair(matrix, sum): res = [] n = len(matrix) for i in range(n): matrix[i].sort() for i in range(n - 1): for j in range(i + 1, n): low = 0 high = n - 1 while (low < n and high >= 0): if ((matrix[i][low] + matrix[j][high]) == sum): pair = (matrix[i][low],matrix[j][high]) res.append(pair) low += 1 high -= 1 else: if ((matrix[i][low] + matrix[j][high]) < sum): low += 1 else: high -= 1 return res sum = 13 matrix = [ [2, 4, 3, 5], [6, 9, 8, 7], [10, 11, 14, 12], [13, 1, 15, 16]] print(sum_pair(matrix, sum))
Input
[[2, 4, 3, 5], [6, 9, 8, 7], [10, 11, 14, 12], [13, 1, 15, 16]] sum = 13
Output
[(4, 9), (5, 8), (2, 11), (3, 10), (12, 1)]
- Related Articles
- Find pairs with given sum such that pair elements lie in different BSTs in Python
- Python - Make pair from two list such that elements are not same in pairs
- Program to find number of pairs (i, j) such that ith and jth elements are same in Python
- Find three element from different three arrays such that that a + b + c = sum in Python
- Find a pair with given sum in BST in C++
- Python Program to Filter Rows with a specific Pair Sum
- Maximum sum such that no two elements are adjacent in C++
- Find pairs with given sum in doubly linked list in C++
- Count pairs with given sum in C++
- Find sub-arrays from given two arrays such that they have equal sum in Python
- Program to find lowest sum of pairs greater than given target in Python
- Find all the pairs with given sum in a BST in C++
- Find the Pair with Given Sum in a Matrix using C++
- Find sum of frequency of given elements in the list in Python
- Find a pair with given sum in a Balanced BST in C++
