Find all permuted rows of a given row in a matrix
Last Updated :
11 Sep, 2023
We are given an m*n matrix of positive integers and a row number. The task is to find all rows in given matrix which are permutations of given row elements. It is also given that values in every row are distinct.
Examples:
Input : mat[][] = {{3, 1, 4, 2},
{1, 6, 9, 3},
{1, 2, 3, 4},
{4, 3, 2, 1}}
row = 3
Output: 0, 2
Rows at indexes 0 and 2 are permutations of
row at index 3.
A simple solution is to one by one sort all rows and check all rows. If any row is completely equal to the given row, that means the current row is a permutation of the given row. The time complexity for this approach will be O(m*n log n).
An efficient approach is to use hashing. Simply create a hash set for the given row. After hash set creation, traverse through the remaining rows, and for every row check if all of its elements are present in the hash set or not.
Implementation:
CPP
#include<bits/stdc++.h>
#define MAX 100
using namespace std;
void permutatedRows( int mat[][MAX], int m, int n, int r)
{
unordered_set< int > s;
for ( int j=0; j<n; j++)
s.insert(mat[r][j]);
for ( int i=0; i<m; i++)
{
if (i==r)
continue ;
int j;
for (j=0; j<n; j++)
if (s.find(mat[i][j]) == s.end())
break ;
if (j != n)
continue ;
cout << i << ", " ;
}
}
int main()
{
int m = 4, n = 4,r = 3;
int mat[][MAX] = {{3, 1, 4, 2},
{1, 6, 9, 3},
{1, 2, 3, 4},
{4, 3, 2, 1}};
permutatedRows(mat, m, n, r);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int MAX = 100 ;
static void permutatedRows( int mat[][], int m, int n, int r)
{
LinkedHashSet<Integer> s = new LinkedHashSet<>();
for ( int j = 0 ; j < n; j++)
s.add(mat[r][j]);
for ( int i = 0 ; i < m; i++)
{
if (i == r)
continue ;
int j;
for (j = 0 ; j < n; j++)
if (!s.contains(mat[i][j]))
break ;
if (j != n)
continue ;
System.out.print(i+ ", " );
}
}
public static void main(String[] args)
{
int m = 4 , n = 4 ,r = 3 ;
int mat[][] = {{ 3 , 1 , 4 , 2 },
{ 1 , 6 , 9 , 3 },
{ 1 , 2 , 3 , 4 },
{ 4 , 3 , 2 , 1 }};
permutatedRows(mat, m, n, r);
}
}
|
Python3
def permutatedRows(mat, m, n, r):
s = set ()
for j in range (n):
s.add(mat[r][j])
for i in range (m):
if i = = r:
continue
for j in range (n):
if mat[i][j] not in s:
j = j - 2
break ;
if j + 1 ! = n:
continue
print (i)
m = 4
n = 4
r = 3
mat = [[ 3 , 1 , 4 , 2 ],
[ 1 , 6 , 9 , 3 ],
[ 1 , 2 , 3 , 4 ],
[ 4 , 3 , 2 , 1 ]]
permutatedRows(mat, m, n, r)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int MAX = 100;
static void permutatedRows( int [,]mat, int m, int n, int r)
{
HashSet< int > s = new HashSet< int >();
for ( int j = 0; j < n; j++)
s.Add(mat[r, j]);
for ( int i = 0; i < m; i++)
{
if (i == r)
continue ;
int j;
for (j = 0; j < n; j++)
if (!s.Contains(mat[i,j]))
break ;
if (j != n)
continue ;
Console.Write(i+ ", " );
}
}
public static void Main(String[] args)
{
int m = 4, n = 4,r = 3;
int [,]mat = {{3, 1, 4, 2},
{1, 6, 9, 3},
{1, 2, 3, 4},
{4, 3, 2, 1}};
permutatedRows(mat, m, n, r);
}
}
|
Javascript
<script>
let MAX = 100;
function permutatedRows(mat, m, n, r)
{
let s = new Set();
for (let j = 0; j < n; j++)
s.add(mat[r][j]);
for (let i = 0; i < m; i++)
{
if (i == r)
continue ;
let j;
for (j = 0; j < n; j++)
if (!s.has(mat[i][j]))
break ;
if (j != n)
continue ;
document.write(i+ ", " );
}
}
let m = 4, n = 4,r = 3;
let mat = [[ 3, 1, 4, 2],
[1, 6, 9, 3],
[1, 2, 3, 4],
[4, 3, 2, 1]];
permutatedRows(mat, m, n, r);
</script>
|
Time complexity: O(m*n)
Auxiliary space: O(n)
Another approach to the solution is using the Standard Template Library(STL):
CPP
#include<bits/stdc++.h>
#define MAX 100
using namespace std;
void permutatedRows( int mat[][MAX], int m, int n, int r)
{
for ( int i=0; i<m&&i!=r; i++){
if (is_permutation(mat[i],mat[i]+n,mat[r])) cout<<i<< "," ;
}
}
int main()
{
int m = 4, n = 4,r = 3;
int mat[][MAX] = {{3, 1, 4, 2},
{1, 6, 9, 3},
{1, 2, 3, 4},
{4, 3, 2, 1}};
permutatedRows(mat, m, n, r);
return 0;
}
|
Java
import java.util.*;
class gfg {
static boolean is_permutation( int [] a, int [] b)
{
Arrays.sort(a);
Arrays.sort(b);
for ( int i = 0 ; i < a.length; i++) {
if (a[i] != b[i])
return false ;
}
return true ;
}
static void permutatedRows( int [][] mat, int m, int n,
int r)
{
for (var i = 0 ; i < m && i != r; i++) {
if (is_permutation(mat[i], mat[r]))
System.out.print(i + "," );
}
}
public static void main(String[] args)
{
int m = 4 , n = 4 , r = 3 ;
int [][] mat = { { 3 , 1 , 4 , 2 },
{ 1 , 6 , 9 , 3 },
{ 1 , 2 , 3 , 4 },
{ 4 , 3 , 2 , 1 } };
permutatedRows(mat, m, n, r);
}
}
|
Python3
MAX = 100
def is_permutation(a, b):
return sorted (a) = = sorted (b)
def permutatedRows(mat, m, n, r):
for i in range ( min (m, r)):
if is_permutation(mat[i], mat[r]):
print (i, end = ", " )
m = 4
n = 4
r = 3 ;
mat = [[ 3 , 1 , 4 , 2 ], [ 1 , 6 , 9 , 3 ], [ 1 , 2 , 3 , 4 ], [ 4 , 3 , 2 , 1 ]];
permutatedRows(mat, m, n, r);
|
C#
using System;
using System.Collections.Generic;
class gfg
{
static bool is_permutation( int [] a, int [] b)
{
Array.Sort(a);
Array.Sort(b);
for ( int i = 0; i < a.Length; i++) {
if (a[i] != b[i])
return false ;
}
return true ;
}
static void permutatedRows( int [][] mat, int m, int n,
int r)
{
for ( var i = 0; i < m && i != r; i++) {
if (is_permutation(mat[i], mat[r]))
Console.Write(i + "," );
}
}
public static void Main( string [] args)
{
int m = 4, n = 4, r = 3;
int [][] mat = { new [] { 3, 1, 4, 2 },
new [] { 1, 6, 9, 3 },
new [] { 1, 2, 3, 4 },
new [] { 4, 3, 2, 1 } };
permutatedRows(mat, m, n, r);
}
}
|
Javascript
let MAX = 100
function is_permutation(a, b)
{
a.sort()
b.sort()
return (a.join( "#" )) == (b.join( "#" ))
}
function permutatedRows(mat, m, n, r)
{
for ( var i=0; i<m&&i!=r; i++){
if (is_permutation(mat[i], mat[r]))
process.stdout.write(i + "," );
}
}
let m = 4, n = 4,r = 3;
let mat = [[ 3, 1, 4, 2],
[1, 6, 9, 3],
[1, 2, 3, 4],
[4, 3, 2, 1]];
permutatedRows(mat, m, n, r);
|
Time Complexity: O(m*n), where m is the number of rows and n is the size of each row. We need to compare each row with the given row, so the time complexity is O(m*n).
Auxiliary Space : O(1). No extra space is used.
Exercise :
Extend the above solution to work for an input matrix where all elements of a row don’t have to be distinct. (Hit: We can use Hash Map instead of a Hash Set)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...