Check if matrix can be converted to another matrix by transposing square sub-matrices
Given two N X M matrices of integers. In an operation, we can transpose any square sub-matrix in matrix1. The task is to check if matrix1 can be converted to matrix2 with the given operation.
Examples:
Input: matrix1[][] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}},
matrix2[][] = {
{1, 4, 7},
{2, 5, 6},
{3, 8, 9}}
Output: Yes
Transpose the whole matrix and then we transpose the
sub-matrix with corners in cells (2, 2) and (3, 3).
Input: matrix1[][] = {
{1, 2},
{3, 4}},
matrix2[][] = {
{1, 4},
{3, 8}}
Output: No
Approach: Sort the diagonals of both the matrices and compare them. If both matrices are equal after sorting diagonals, then matrix1 can be converted to matrix2. The following method is correct because of the fact that while transposing the numbers can only be transposed to any of its diagonal.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
#define n 3
#define m 3
bool check( int a[n][m], int b[n][m])
{
for ( int i = 0; i < n; i++) {
vector< int > v1, v2;
int r = i;
int col = 0;
while (r >= 0 && col < m) {
v1.push_back(a[r][col]);
v2.push_back(b[r][col]);
r--;
col++;
}
sort(v1.begin(), v1.end());
sort(v2.begin(), v2.end());
for ( int i = 0; i < v1.size(); i++) {
if (v1[i] != v2[i])
return false ;
}
}
for ( int j = 1; j < m; j++) {
vector< int > v1, v2;
int r = n - 1;
int col = j;
while (r >= 0 && col < m) {
v1.push_back(a[r][col]);
v2.push_back(b[r][col]);
r--;
col++;
}
sort(v1.begin(), v1.end());
sort(v2.begin(), v2.end());
for ( int i = 0; i < v1.size(); i++) {
if (v1[i] != v2[i])
return false ;
}
}
return true ;
}
int main()
{
int a[n][m] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int b[n][m] = { { 1, 4, 7 }, { 2, 5, 6 }, { 3, 8, 9 } };
if (check(a, b))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static final int n = 3 ;
static final int m = 3 ;
static boolean check( int a[][], int b[][])
{
for ( int i = 0 ; i < n; i++)
{
Vector<Integer> v1 = new Vector<Integer>();
Vector<Integer> v2 = new Vector<Integer>();
int r = i;
int col = 0 ;
while (r >= 0 && col < m)
{
v1.add(a[r][col]);
v2.add(b[r][col]);
r--;
col++;
}
Collections.sort(v1);
Collections.sort(v2);
for ( int j = 0 ; j < v1.size(); j++)
{
if (v1.get(j) != v2.get(j))
{
return false ;
}
}
}
for ( int j = 1 ; j < m; j++)
{
Vector<Integer> v1 = new Vector<Integer>();
Vector<Integer> v2 = new Vector<Integer>();
int r = n - 1 ;
int col = j;
while (r >= 0 && col < m)
{
v1.add(a[r][col]);
v2.add(b[r][col]);
r--;
col++;
}
Collections.sort(v1);
Collections.sort(v2);
for ( int i = 0 ; i < v1.size(); i++)
{
if (v1.get(i) != v2.get(i))
{
return false ;
}
}
}
return true ;
}
public static void main(String[] args)
{
int a[][] = {{ 1 , 2 , 3 }, { 4 , 5 , 6 }, { 7 , 8 , 9 }};
int b[][] = {{ 1 , 4 , 7 }, { 2 , 5 , 6 }, { 3 , 8 , 9 }};
if (check(a, b))
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
}
|
Python3
n = 3
m = 3
def check(a, b):
for i in range (n):
v1 = []
v2 = []
r = i
col = 0
while (r > = 0 and col < m):
v1.append(a[r][col])
v2.append(b[r][col])
r - = 1
col + = 1
v1.sort(reverse = False )
v2.sort(reverse = False )
for i in range ( len (v1)):
if (v1[i] ! = v2[i]):
return False
for j in range ( 1 , m):
v1 = []
v2 = []
r = n - 1
col = j
while (r > = 0 and col < m):
v1.append(a[r][col])
v2.append(b[r][col])
r - = 1
col + = 1
v1.sort(reverse = False )
v2.sort(reverse = False )
for i in range ( len (v1)):
if (v1[i] ! = v2[i]):
return False
return True
if __name__ = = '__main__' :
a = [[ 1 , 2 , 3 ], [ 4 , 5 , 6 ], [ 7 , 8 , 9 ]]
b = [[ 1 , 4 , 7 ], [ 2 , 5 , 6 ], [ 3 , 8 , 9 ]]
if (check(a, b)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static readonly int n = 3;
static readonly int m = 3;
static bool check( int [,]a, int [,]b)
{
for ( int i = 0; i < n; i++)
{
List< int > v1 = new List< int >();
List< int > v2 = new List< int >();
int r = i;
int col = 0;
while (r >= 0 && col < m)
{
v1.Add(a[r, col]);
v2.Add(b[r, col]);
r--;
col++;
}
v1.Sort();
v2.Sort();
for ( int j = 0; j < v1.Count; j++)
{
if (v1[j] != v2[j])
{
return false ;
}
}
}
for ( int j = 1; j < m; j++)
{
List< int > v1 = new List< int >();
List< int > v2 = new List< int >();
int r = n - 1;
int col = j;
while (r >= 0 && col < m)
{
v1.Add(a[r, col]);
v2.Add(b[r, col]);
r--;
col++;
}
v1.Sort();
v2.Sort();
for ( int i = 0; i < v1.Count; i++)
{
if (v1[i] != v2[i])
{
return false ;
}
}
}
return true ;
}
public static void Main()
{
int [,]a = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int [,]b = {{1, 4, 7}, {2, 5, 6}, {3, 8, 9}};
if (check(a, b))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
|
PHP
<?php
$n = 3;
$m = 3;
function check( $a , $b )
{
global $n , $m ;
for ( $i = 0; $i < $n ; $i ++)
{
$v1 = array ();
$v2 = array ();
$r = $i ;
$col = 0;
while ( $r >= 0 && $col < $m )
{
array_push ( $v1 , $a [ $r ][ $col ]);
array_push ( $v2 , $b [ $r ][ $col ]);
$r --;
$col ++;
}
sort( $v1 );
sort( $v2 );
for ( $i = 0; $i < count ( $v1 ); $i ++)
{
if ( $v1 [ $i ] != $v2 [ $i ])
return false;
}
}
for ( $j = 1; $j < $m ; $j ++)
{
$v1 = array ();
$v2 = array ();
$r = $n - 1;
$col = $j ;
while ( $r >= 0 && $col < $m )
{
array_push ( $v1 , $a [ $r ][ $col ]);
array_push ( $v2 , $b [ $r ][ $col ]);
$r --;
$col ++;
}
sort( $v1 );
sort( $v2 );
for ( $i = 0; $i < count ( $v1 ); $i ++)
{
if ( $v1 [ $i ] != $v2 [ $i ])
return false;
}
}
return true;
}
$a = array ( array ( 1, 2, 3 ),
array ( 4, 5, 6 ),
array ( 7, 8, 9 ));
$b = array ( array ( 1, 4, 7 ),
array ( 2, 5, 6 ),
array ( 3, 8, 9 ));
if (check( $a , $b ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
var n = 3
var m = 3
function check(a, b)
{
for ( var i = 0; i < n; i++) {
var v1 = [], v2 = [];
var r = i;
var col = 0;
while (r >= 0 && col < m) {
v1.push(a[r][col]);
v2.push(b[r][col]);
r--;
col++;
}
v1.sort();
v2.sort();
for ( var i = 0; i < v1.length; i++) {
if (v1[i] != v2[i])
return false ;
}
}
for ( var j = 1; j < m; j++) {
var v1 = [], v2 = [];
var r = n - 1;
var col = j;
while (r >= 0 && col < m) {
v1.push(a[r][col]);
v2.push(b[r][col]);
r--;
col++;
}
v1.sort();
v2.sort();
for ( var i = 0; i < v1.length; i++) {
if (v1[i] != v2[i])
return false ;
}
}
return true ;
}
var a = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ];
var b = [ [ 1, 4, 7 ], [ 2, 5, 6 ], [ 3, 8, 9 ] ];
if (check(a, b))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(max(N *M*logM, N*M * logN)) which can be written as O(N*M*max(logN,logM)), as we are using a loop to traverse N times and we are using sort function on an array of size M which will cost O(M*logM) and we are also traversing M times and sorting an array of size N which will cost O(N*logN).
Auxiliary Space: O(max(N,M)), as we are using extra space for the array v1 and v2.
Last Updated :
18 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...