Given a matrix mat[][] of size m × n and a positive integer ind (0 ≤ ind < m), determine all row indices i such that the elements of row i form a permutation of the elements in row ind.
It is guaranteed that every row contains distinct elements.
Note: Two rows are considered permutations of each other if they contain the same elements in any order.
Examples:
Input: ind = 3 , mat[][] = [[3, 1, 4, 2],
[1, 6, 9, 3],
[1, 2, 3, 4],
[4, 3, 2, 1]]
Output: 0 2
Explanation: Rows at indexes 0 and 2 are permutations of row at index 3.Input : ind = 0, mat[][] = [[1, 2],
[2, 1]]
Output: 1
Explanation: Row at indexes 1 is permutation of row at index 0.
Table of Content
[Naive Approach] - Using Sorting - O(m × n × log n) Time and O(1) Space
The idea is to sort all the rows of matrix mat[][] and check all the rows. If any row is completely equal to the row at index ind, it means the row is a permutation of the given row.
#include<iostream>
#include<vector>
using namespace std;
vector<int> permutedRows(vector<vector<int>>& mat, int ind) {
vector<int>result;
// sort all the rows
for(int i = 0; i<mat.size(); i++){
sort(mat[i].begin(), mat[i].end());
}
for(int i = 0; i<mat.size(); i++){
// skip if the i is equal to ind
if(i == ind)
continue;
// check if the row is permuted or not
if(mat[i] == mat[ind]){
result.push_back(i);
}
}
return result;
}
int main() {
vector<vector<int>> mat = {{3, 1, 4, 2},
{1, 6, 9, 3},
{1, 2, 3, 4},
{4, 3, 2, 1}};
int ind = 3;
vector<int>result=permutedRows(mat, ind);
for(auto&i:result){
cout<<i<<" ";
}
return 0;
}
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
class GfG {
static List<Integer> permutedRows(List<List<Integer>> mat, int ind) {
List<Integer> result = new ArrayList<>();
// sort all the rows
for (int i = 0; i < mat.size(); i++) {
Collections.sort(mat.get(i));
}
for (int i = 0; i < mat.size(); i++) {
// skip if the i is equal to ind
if (i == ind)
continue;
// check if the row is permuted or not
if (mat.get(i).equals(mat.get(ind))) {
result.add(i);
}
}
return result;
}
public static void main(String[] args) {
List<List<Integer>> mat = new ArrayList<>();
mat.add(new ArrayList<>(Arrays.asList(3, 1, 4, 2)));
mat.add(new ArrayList<>(Arrays.asList(1, 6, 9, 3)));
mat.add(new ArrayList<>(Arrays.asList(1, 2, 3, 4)));
mat.add(new ArrayList<>(Arrays.asList(4, 3, 2, 1)));
int ind = 3;
List<Integer> result = permutedRows(mat, ind);
for (int i : result)
System.out.print(i + " ");
}
}
def permutedRows(mat, ind):
result = []
# sort all the rows
for i in range(len(mat)):
mat[i].sort()
for i in range(len(mat)):
# skip if the i is equal to ind
if i == ind:
continue
# check if the row is permuted or not
if mat[i] == mat[ind]:
result.append(i)
return result
if __name__ == "__main__":
mat = [
[3, 1, 4, 2],
[1, 6, 9, 3],
[1, 2, 3, 4],
[4, 3, 2, 1]
]
ind = 3
result = permutedRows(mat, ind)
for i in result:
print(i, end=" ")
using System;
using System.Collections.Generic;
class GfG
{
static List<int> PermutedRows(List<List<int>> mat, int ind)
{
List<int> result = new List<int>();
// sort all the rows
for (int i = 0; i < mat.Count; i++)
{
mat[i].Sort();
}
for (int i = 0; i < mat.Count; i++)
{
// skip if the i is equal to ind
if (i == ind)
continue;
// check if the row is permuted or not
if (AreEqual(mat[i], mat[ind]))
{
result.Add(i);
}
}
return result;
}
static bool AreEqual(List<int> a, List<int> b)
{
if (a.Count != b.Count) return false;
for (int i = 0; i < a.Count; i++)
{
if (a[i] != b[i])
return false;
}
return true;
}
static void Main()
{
var mat = new List<List<int>>
{
new List<int>{3,1,4,2},
new List<int>{1,6,9,3},
new List<int>{1,2,3,4},
new List<int>{4,3,2,1}
};
int ind = 3;
var result = PermutedRows(mat, ind);
foreach (var i in result)
Console.Write(i + " ");
}
}
function permutedRows(mat, ind) {
let result = [];
// sort all the rows
for (let i = 0; i < mat.length; i++) {
mat[i].sort((a, b) => a - b);
}
for (let i = 0; i < mat.length; i++) {
// skip if the i is equal to ind
if (i === ind)
continue;
// check if the row is permuted or not
if (mat[i].toString() === mat[ind].toString()) {
result.push(i);
}
}
return result;
}
// Driver code
let mat = [
[3, 1, 4, 2],
[1, 6, 9, 3],
[1, 2, 3, 4],
[4, 3, 2, 1]
];
let ind = 3;
let result = permutedRows(mat, ind);
console.log(result.join(" "));
Output
0 2
[Expected Approach] - Using HashSet - O(m × n) Time and O(n) Space
The idea is to create a HashSet for the row with index ind in the matrix mat[][], then traverse through all the rows and check if all of its elements are present in the HashSet or not.
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
vector<int> permutedRows(vector<vector<int>>& mat, int ind) {
vector<int> result;
// Store elements of row ind
unordered_set<int> s(mat[ind].begin(), mat[ind].end());
// Traverse through all rows in the matrix
for (int i = 0; i < mat.size(); i++) {
if (i == ind) continue;
// Check if all elements in the current
// row exist in the set
bool isPermutation = true;
for (int x : mat[i]) {
// Element not in the set
if (s.find(x) == s.end()) {
isPermutation = false;
break;
}
}
if (isPermutation) {
result.push_back(i);
}
}
return result;
}
int main() {
vector<vector<int>> mat = {{3, 1, 4, 2},
{1, 6, 9, 3},
{1, 2, 3, 4},
{4, 3, 2, 1}};
int ind = 3;
vector<int> result = permutedRows(mat, ind);
for (int i : result)
cout << i << " ";
return 0;
}
import java.util.List;
import java.util.ArrayList;
import java.util.HashSet;
public class GfG {
static List<Integer> permutedRows(int[][] mat, int ind) {
List<Integer> result = new ArrayList<>();
// Store elements of row ind
HashSet<Integer> s = new HashSet<>();
for (int x : mat[ind])
s.add(x);
// Traverse through all rows in the matrix
for (int i = 0; i < mat.length; i++) {
if (i == ind) continue;
// Check if all elements in the current
// row exist in the set
boolean isPermutation = true;
for (int x : mat[i]) {
// Element not in the set
if (!s.contains(x)) {
isPermutation = false;
break;
}
}
if (isPermutation) {
result.add(i);
}
}
return result;
}
public static void main(String[] args) {
int[][] mat = {
{3, 1, 4, 2},
{1, 6, 9, 3},
{1, 2, 3, 4},
{4, 3, 2, 1}
};
int ind = 3;
List<Integer> result = permutedRows(mat, ind);
for (int i : result)
System.out.print(i + " ");
}
}
def permutedRows(mat, ind):
result = []
# Store elements of row ind
s = set(mat[ind])
# Traverse through all rows in the matrix
for i in range(len(mat)):
if i == ind:
continue
# Check if all elements in the current
# row exist in the set
isPermutation = True
for x in mat[i]:
# Element not in the set
if x not in s:
isPermutation = False
break
if isPermutation:
result.append(i)
return result
if __name__ == "__main__":
mat = [
[3, 1, 4, 2],
[1, 6, 9, 3],
[1, 2, 3, 4],
[4, 3, 2, 1]
]
ind = 3
result = permutedRows(mat, ind)
for i in result:
print(i, end=" ")
using System;
using System.Collections.Generic;
class GfG {
static List<int> permutedRows(int[][] mat, int ind) {
List<int> result = new List<int>();
// Store elements of row ind
HashSet<int> s = new HashSet<int>(mat[ind]);
// Traverse through all rows in the matrix
for (int i = 0; i < mat.Length; i++) {
if (i == ind) continue;
// Check if all elements in the current
// row exist in the set
bool isPermutation = true;
foreach (int x in mat[i]) {
// Element not in the set
if (!s.Contains(x)) {
isPermutation = false;
break;
}
}
if (isPermutation) {
result.Add(i);
}
}
return result;
}
static void Main() {
int[][] mat = {
new int[]{3, 1, 4, 2},
new int[]{1, 6, 9, 3},
new int[]{1, 2, 3, 4},
new int[]{4, 3, 2, 1}
};
int ind = 3;
List<int> result = permutedRows(mat, ind);
foreach (int i in result)
Console.Write(i + " ");
}
}
function permutedRows(mat, ind) {
let result = [];
// Store elements of row ind
let s = new Set(mat[ind]);
// Traverse through all rows in the matrix
for (let i = 0; i < mat.length; i++) {
if (i === ind) continue;
// Check if all elements in the current
// row exist in the set
let isPermutation = true;
for (let x of mat[i]) {
// Element not in the set
if (!s.has(x)) {
isPermutation = false;
break;
}
}
if (isPermutation) {
result.push(i);
}
}
return result;
}
// Driver code
let mat = [
[3, 1, 4, 2],
[1, 6, 9, 3],
[1, 2, 3, 4],
[4, 3, 2, 1]
];
let ind = 3;
let result = permutedRows(mat, ind);
for (let i of result)
process.stdout.write(i + " ");
Output
0 2
Note : In C++, this problem can be efficiently solved using the is_permutation function from the <algorithm> library. It checks whether two ranges contain the same elements in any order. Using this, we can compare the row at index ind with each row of the matrix to find all permuted rows.
Exercise: Extend the above solution to work for an input matrix where all elements of a row don’t have to be distinct. (Hint: We can use Hash Map instead of a Hash Set)