Check if it is possible to make two matrices strictly increasing by swapping corresponding values only
Last Updated :
22 Apr, 2022
Given two n * m matrices A[][] and B[][], the task is to make both the matrices strictly increasing (both rows and columns) only by swapping two elements in different matrices if they are located in the corresponding position i.e. A[i][j] can only be swapped with B[i][j]. If possible then print Yes otherwise, No.
Examples:
Input:
A[][] = {{2, 10}, B[][] = {{9, 4},
{11, 5}} {3, 12}}
Output: Yes
Swap 2 with 9 and 5 with 12 then the resulting
matrices will be strictly increasing.
Input:
A[][] = {{1, 3}, B[][] = {{3, 1},
{2, 4}, {3, 6},
{5, 10}} {4, 8}}
Output: No
Approach: We can solve this problem using greedy technique. Swap A[i][j] with B[i][j] if A[i][j] > B[i][j]. At the end for every i and j we have A[i][j] ? B[i][j].
If the resultant matrices are strictly increasing then print Yes otherwise, print No.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
string Check( int a[][2], int b[][2], int n, int m)
{
for ( int i = 0; i < n; i++)
for ( int j = 0; j < m; j++)
if (a[i][j] > b[i][j])
swap(a[i][j], b[i][j]);
for ( int i = 0; i < n; i++)
for ( int j = 0; j < m - 1; j++)
if (a[i][j] >= a[i][j + 1] || b[i][j] >= b[i][j + 1])
return "No" ;
for ( int i = 0; i < n - 1; i++)
for ( int j = 0; j < m ;j++)
if (a[i][j] >= a[i + 1][j] || b[i][j] >= b[i + 1][j])
return "No" ;
return "Yes" ;
}
int main()
{
int n = 2, m = 2;
int a[][2] = {{2, 10}, {11, 5}};
int b[][2] = {{9, 4}, {3, 12}};
cout << (Check(a, b,n,m));
}
|
Java
class GFG{
public static String Check( int a[][], int b[][],
int n, int m)
{
for ( int i = 0 ; i < n; i++)
for ( int j = 0 ; j < m; j++)
if (a[i][j] > b[i][j])
{
int temp = a[i][j];
a[i][j] = b[i][j];
b[i][j] = temp;
}
for ( int i = 0 ; i < n; i++)
for ( int j = 0 ; j < m - 1 ; j++)
if (a[i][j] >= a[i][j + 1 ] ||
b[i][j] >= b[i][j + 1 ])
return "No" ;
for ( int i = 0 ; i < n - 1 ; i++)
for ( int j = 0 ; j < m ;j++)
if (a[i][j] >= a[i + 1 ][j] ||
b[i][j] >= b[i + 1 ][j])
return "No" ;
return "Yes" ;
}
public static void main(String[] args)
{
int n = 2 , m = 2 ;
int a[][] = { { 2 , 10 }, { 11 , 5 } };
int b[][] = { { 9 , 4 }, { 3 , 12 } };
System.out.print(Check(a, b, n, m));
}
}
|
Python3
def Check(a, b):
for i in range (n):
for j in range (m):
if a[i][j]>b[i][j]:
a[i][j], b[i][j] = b[i][j], a[i][j]
for i in range (n):
for j in range (m - 1 ):
if (a[i][j]> = a[i][j + 1 ] or b[i][j]> = b[i][j + 1 ]):
return "No"
for i in range (n - 1 ):
for j in range (m):
if (a[i][j]> = a[i + 1 ][j] or b[i][j]> = b[i + 1 ][j]):
return "No"
return "Yes"
if __name__ = = "__main__" :
n, m = 2 , 2
a = [[ 2 , 10 ], [ 11 , 5 ]]
b = [[ 9 , 4 ], [ 3 , 12 ]]
print (Check(a, b))
|
C#
using System;
using System.Collections;
class GfG{
static string Check( int [,]a, int [,]b,
int n, int m)
{
for ( int i = 0; i < n; i++)
for ( int j = 0; j < m; j++)
if (a[i, j] > b[i, j])
{
int tmp = a[i, j];
a[i, j] = b[i, j];
b[i, j] = tmp;
}
for ( int i = 0; i < n; i++)
for ( int j = 0; j < m - 1; j++)
if (a[i, j] >= a[i, j + 1] ||
b[i, j] >= b[i, j + 1])
return "No" ;
for ( int i = 0; i < n - 1; i++)
for ( int j = 0; j < m ; j++)
if (a[i, j] >= a[i + 1, j] ||
b[i, j] >= b[i + 1, j])
return "No" ;
return "Yes" ;
}
public static void Main( string []arg)
{
int n = 2, m = 2;
int [,]a = {{2, 10}, {11, 5}};
int [,]b = {{9, 4}, {3, 12}};
Console.Write(Check(a, b, n, m));
}
}
|
PHP
<?php
function Check( $a , $b , $n , $m )
{
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = 0; $j < $n ; $j ++)
{
if ( $a [ $i ][ $j ] > $b [ $i ][ $j ])
{
$temp = $a [ $i ][ $j ];
$a [ $i ][ $j ] = $b [ $i ][ $j ];
$b [ $i ][ $j ] = $temp ;
}
}
}
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = 0; $j < $m -1 ; $j ++)
{
if ( $a [ $i ][ $j ] >= $a [ $i ][ $j + 1] or
$b [ $i ][ $j ] >= $b [ $i ][ $j + 1])
return "No" ;
}
}
for ( $i = 0; $i < $n - 1; $i ++)
{
for ( $j = 0; $j < $m ; $j ++)
{
if ( $a [ $i ][ $j ] >= $a [ $i + 1][ $j ] or
$b [ $i ][ $j ] >= $b [ $i + 1][ $j ])
return "No" ;
}
}
return "Yes" ;
}
$n = 2; $m = 2;
$a = array ( array (2, 10), array (11, 5));
$b = array ( array (9, 4), array (3, 12));
print (Check( $a , $b , $n , $m ));
?>
|
Javascript
<script>
function Check(a, b, n, m)
{
for (let i = 0; i < n; i++)
for (let j = 0; j < m; j++)
if (a[i][j] > b[i][j])
{
let temp = a[i][j];
a[i][j] = b[i][j];
b[i][j] = temp;
}
for (let i = 0; i < n; i++)
for (let j = 0; j < m - 1; j++)
if (a[i][j] >= a[i][j + 1] ||
b[i][j] >= b[i][j + 1])
return "No" ;
for (let i = 0; i < n - 1; i++)
for (let j = 0; j < m ;j++)
if (a[i][j] >= a[i + 1][j] ||
b[i][j] >= b[i + 1][j])
return "No" ;
return "Yes" ;
}
let n = 2, m = 2;
let a = [[ 2, 10 ], [ 11, 5 ]];
let b = [[ 9, 4 ], [3, 12 ]];
document.write(Check(a, b, n, m));
</script>
|
Time Complexity: O(N*M), as we are traversing over the matrix.
Auxiliary Space: O(1), as we are not using any extra space.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...