Find pairs with given sum such that elements of pair are in different rows - GeeksforGeeks
Given a matrix of distinct values and a sum. The task is to find all the pairs in given whose summation is equal to given sum. Each element of pair must be from different rows i.e; pair must not lie in same row.
Time complexity : O(n2logn + n^3)
A simple solution for this problem is to one by one take each element of all rows and find pair starting from immediate next row with in matrix. Time complexity for this approach will be O(n4).
Read full article from Find pairs with given sum such that elements of pair are in different rows - GeeksforGeeks
Given a matrix of distinct values and a sum. The task is to find all the pairs in given whose summation is equal to given sum. Each element of pair must be from different rows i.e; pair must not lie in same row.
Time Complexity : O(n2) under the assumption that hash table insert and search operations take O(1) time.
- Create an empty hash table and store all elements of matrix in hash as key and their locations as values.
- Traverse the matrix again to check for every element whether its pair exists in hash table or not. If exists, then compare row numbers. If row numbers are not same, then print the pair.
- this assumes every number is unqiue
if not, we can use map<row, map<value, colum>>
void
pairSum(
int
mat[][MAX],
int
n,
int
sum)
{
// Create a hash and store all elements of matrix
// as keys, and row and column numbers as values
unordered_map<
int
, pair<
int
,
int
> > hm;
for
(
int
i=0; i<n; i++)
for
(
int
j=0; j<n; j++)
hm[mat[i][j]] = make_pair(i, j);
// Traverse the matrix again to check for every
// element whether its pair exists or not.
for
(
int
i=0; i<n; i++)
{
for
(
int
j=0; j<n; j++)
{
// Look for remaining sum in hash
int
remSum = sum - mat[i][j];
auto
it = hm.find(remSum);
// it is an iterator
// of unordered_map type
// If an element with value equal to remaining sum exists
if
(it != hm.end())
{
// Find row and column numbers of element with
// value equal to remaining sum.
pair<
int
,
int
> p = it->second;
int
row = p.first, col = p.second;
// If row number of pair is not same as current
// row, then print it as part of result.
// Second condition is there to make sure that a
// pair is printed only once.
if
(row != i && row > i)
cout <<
"("
<< mat[i][j] <<
", "
<< mat[row][col] <<
"), "
;
}
}
}
}
Time complexity : O(n2logn + n^3)
- Sort all the rows in ascending order. Time complexity for this preprocessing will be O(n2 logn).
- Now we will select each row one by one and find pair element in remaining rows after current row.
- Take two iterators left and right. left iterator points left corner of current i’th row and right iterator points right corner of next j’th row in which we are going to find pair element.
- If mat[i][left] + mat[j][right] < sum then left++ i.e; move in i’th row towards right corner, otherwise right++ i.e; move in j’th row towards left corner.
void
pairSum(
int
mat[][MAX],
int
n,
int
sum)
{
// First sort all the rows in ascending order
for
(
int
i=0; i<n; i++)
sort(mat[i], mat[i]+n);
// Select i'th row and find pair for element in i'th
// row in j'th row whose summation is equal to given sum
for
(
int
i=0; i<n-1; i++)
{
for
(
int
j=i+1; j<n; j++)
{
int
left = 0, right = n-1;
while
(left<n && right>=0)
{
if
((mat[i][left] + mat[j][right]) == sum)
{
cout <<
"("
<< mat[i][left]
<<
", "
<< mat[j][right] <<
"), "
;
left++;
right--;
}
else
{
if
((mat[i][left] + mat[j][right]) < sum)
left++;
else
right--;
}
}
}
}
}
A simple solution for this problem is to one by one take each element of all rows and find pair starting from immediate next row with in matrix. Time complexity for this approach will be O(n4).
Read full article from Find pairs with given sum such that elements of pair are in different rows - GeeksforGeeks