Given a matrix mat[][] of order m*n, and an index ind. The task is to find all the rows in the matrix mat[][] which are permutations of rows at index ind.
Note: All the elements of a row are distinct.
Examples:
Input: mat[][] = [[3, 1, 4, 2],
[1, 6, 9, 3],
[1, 2, 3, 4],
[4, 3, 2, 1]]ind = 3
Output: 0 2
Explanation: Rows at indexes 0 and 2 are permutations of row at index 3.Input : mat[][] = [[1, 2],
[2, 1]]
ind = 0
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.
// C++ Program to find the permuted rows
// using sorting.
#include<bits/stdc++.h>
using namespace std;
// function to find the permuted rows
void permutedRows(vector<vector<int>>& mat, int ind) {
// sort all the rows of matrix mat[][]
for(int i = 0; i<mat.size(); i++){
sort(mat[i].begin(), mat[i].end());
}
// check for the permuted rows
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]){
cout<<i<<" ";
}
}
}
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;
permutedRows(mat, ind);
return 0;
}
// Java Program to find the permuted rows
// using sorting.
import java.util.*;
class GfG {
// function to find the permuted rows
static void permutedRows(int[][] mat, int ind) {
// sort all the rows of matrix mat[][]
for (int i = 0; i < mat.length; i++) {
Arrays.sort(mat[i]);
}
// check for the permuted rows
for (int 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 (Arrays.equals(mat[i], mat[ind])) {
System.out.print(i + " ");
}
}
}
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;
permutedRows(mat, ind);
}
}
# Python Program to find the permuted rows
# using sorting.
def permutedRows(mat, ind):
# sort all the rows of matrix mat[][]
for i in range(len(mat)):
mat[i].sort()
# check for the permuted rows
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]:
print(i, end=" ")
if __name__ == "__main__":
mat = [[3, 1, 4, 2],
[1, 6, 9, 3],
[1, 2, 3, 4],
[4, 3, 2, 1]]
ind = 3
permutedRows(mat, ind)
// C# Program to find the permuted rows
// using sorting.
using System;
using System.Linq;
class GfG {
// function to find the permuted rows
static void permutedRows(int[][] mat, int ind) {
// sort all the rows of matrix mat[][]
for (int i = 0; i < mat.Length; i++) {
Array.Sort(mat[i]);
}
// check for the permuted rows
for (int 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 (Enumerable.SequenceEqual(mat[i], mat[ind])) {
Console.Write(i + " ");
}
}
}
static void Main(string[] args) {
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;
permutedRows(mat, ind);
}
}
// JavaScript Program to find the permuted rows
// using sorting.
function permutedRows(mat, ind) {
// sort all the rows of matrix mat[][]
for (let i = 0; i < mat.length; i++) {
mat[i].sort((a, b) => a - b);
}
// check for the permuted rows
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
let isPermuted = 1;
for (let j = 0; j < mat[0].length; j++) {
if (mat[i][j] != mat[ind][j]) {
isPermuted = 0;
break;
}
}
if (isPermuted) {
console.log(i + " ");
}
}
}
const mat = [
[3, 1, 4, 2],
[1, 6, 9, 3],
[1, 2, 3, 4],
[4, 3, 2, 1]];
const ind = 3;
permutedRows(mat, ind);
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.
// C++ Program to find the permuted rows
// using hashset.
#include <bits/stdc++.h>
using namespace std;
// Function to find all permuted rows
void permutedRows(vector<vector<int>>& mat, int ind) {
// 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++) {
// Skip the given row
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) {
cout << i << " ";
}
}
}
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;
permutedRows(mat, ind);
return 0;
}
// Java Program to find the permuted rows
// using hashset.
import java.util.HashSet;
import java.util.Set;
class GfG {
// Function to find all permuted rows of a given row ind
static void permutedRows(int[][] mat, int ind) {
// Store elements of row r in a set
Set<Integer> set = new HashSet<>();
for (int val : mat[ind]) {
set.add(val);
}
// Traverse through all rows in the matrix
for (int i = 0; i < mat.length; i++) {
// Skip the given row
if (i == ind) continue;
// Check if all elements in the current
// row exist in the set
boolean isPermutation = true;
for (int val : mat[i]) {
if (!set.contains(val)) {
isPermutation = false;
break;
}
}
if (isPermutation) {
System.out.print(i + " ");
}
}
}
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;
permutedRows(mat, ind);
}
}
# Python Program to find the permuted rows
# using hashset.
def permutedRows(mat, ind):
# Store elements of row ind in a set
s = set(mat[ind])
# Traverse through all rows in the matrix
for i in range(len(mat)):
# Skip the given row
if i == ind:
continue
# Check if all elements in the current
# row exist in the set
isPermutation = True
for val in mat[i]:
if val not in s:
isPermutation = False
break
if isPermutation:
print(i, end=' ')
mat = [
[3, 1, 4, 2],
[1, 6, 9, 3],
[1, 2, 3, 4],
[4, 3, 2, 1]]
ind = 3
permutedRows(mat, ind)
// C# Program to find the permuted rows
// using hashset.
using System;
using System.Collections.Generic;
// Function to find all permuted rows of a given row ind
class GfG {
static void permutedRows(int[,] mat, int ind) {
// Store elements of row ind in a HashSet
HashSet<int> s = new HashSet<int>();
for (int j = 0; j < mat.GetLength(1); j++) {
s.Add(mat[ind, j]);
}
// Traverse through all rows in the matrix
for (int i = 0; i < mat.GetLength(0); i++) {
// Skip the given row
if (i == ind) continue;
// Check if all elements in the current row
// exist in the set
bool isPermutation = true;
for (int j = 0; j < mat.GetLength(1); j++) {
if (!s.Contains(mat[i, j])) {
isPermutation = false;
break;
}
}
if (isPermutation) {
Console.Write(i + " ");
}
}
}
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;
permutedRows(mat, ind);
}
}
// JavaScript Program to find the permuted rows
// using hashset.
function permutedRows(mat, ind) {
// Store elements of row ind in a set
const s = new Set(mat[ind]);
// Traverse through all rows in the matrix
for (let i = 0; i < mat.length; i++) {
// Skip the given row
if (i === ind) {
continue;
}
// Check if all elements in the current
// row exist in the set
let isPermutation = true;
for (const val of mat[i]) {
if (!s.has(val)) {
isPermutation = false;
break;
}
}
if (isPermutation) {
console.log(i + ' ');
}
}
}
const mat = [
[3, 1, 4, 2],
[1, 6, 9, 3],
[1, 2, 3, 4],
[4, 3, 2, 1]];
const ind = 3;
permutedRows(mat, ind);
Output
0 2
In C++, this problem can be efficiently solved using the built-in is_permutation function from the <algorithm> library. This function compares two ranges to check if they contain the same elements in any order. By utilizing this function, we can compare the given row (indexed by ind) with every other row in the matrix to determine if they are permutations of each other.
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)