Check whether the given Matrix is balanced or not
Last Updated :
30 Aug, 2021
Given a matrix mat[][] of dimensions NxM, the task is to check whether the given matrix is balanced or not. Print “Balanced” if it is a balanced matrix else print “Unbalanced”.
A matrix is balanced if all cells in the matrix are balanced and a cell of the matrix is balanced if the number of cells in that matrix that are adjacent to that cell is strictly greater than the value written in this cell.
Adjacent cell means cells in the top, down, left, and right cell of each cell if it exists.
Examples:
Input: N = 3, M = 3
mat[][] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}}
Output: Unbalanced
Explanation: Each cell of the given grid is not stable, so the overall grid is unbalanced.
Input: N = 3, M = 3
mat[][] = {{1, 2, 1},
{2, 3, 2},
{1, 2, 1}}
Output: Balanced
Explanation: Each cell of the given grid is stable, so the overall grid is Balanced.
Approach:
- Traverse the given matrix mat[][].
- For each cell of the matrix check if all the adjacent cells i.e., mat[i+1][j], mat[i][j+1], mat[i-1][j], mat[i][j-1] are strictly smaller than the current cell.
- For the corner cells of the matrix, there are only two adjacent cells i.e., mat[i+1][j] and mat[i][j+1] check if all these adjacent cells are strictly smaller than the corner cell.
- For border cell of the matrix, there are 3 adjacent cells i.e., mat[i-1][j], mat[i+1][j], and mat[i][j+1] check if all these adjacent cells are strictly smaller than the border cell.
- If all the above conditions are true for all the cells of the matrix then print “Balanced” else print “Unbalanced”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define N 4
#define M 4
string balancedMatrix( int mat[][M])
{
bool is_balanced = true ;
for ( int i = 0; i < N && is_balanced; i++) {
for ( int j = 0; j < M && is_balanced; j++) {
if ((i == 0 || i == N - 1)
&& (j == 0 || j == M - 1)) {
if (mat[i][j] >= 2)
is_balanced = false ;
}
else if (i == 0 || i == N - 1
|| j == 0 || j == M - 1) {
if (mat[i][j] >= 3)
is_balanced = false ;
}
else {
if (mat[i][j] >= 4)
is_balanced = false ;
}
}
}
if (is_balanced)
return "Balanced" ;
else
return "Unbalanced" ;
}
int main()
{
int mat[N][M] = { { 1, 2, 3, 4 },
{ 3, 5, 2, 6 },
{ 5, 3, 6, 1 },
{ 9, 5, 6, 0 } };
cout << balancedMatrix(mat);
return 0;
}
|
Java
import java.util.*;
class GFG{
static final int N = 4 ;
static final int M = 4 ;
static String balancedMatrix( int mat[][])
{
boolean is_balanced = true ;
for ( int i = 0 ; i < N && is_balanced; i++)
{
for ( int j = 0 ; j < M && is_balanced; j++)
{
if ((i == 0 || i == N - 1 ) &&
(j == 0 || j == M - 1 ))
{
if (mat[i][j] >= 2 )
is_balanced = false ;
}
else if (i == 0 || i == N - 1 ||
j == 0 || j == M - 1 )
{
if (mat[i][j] >= 3 )
is_balanced = false ;
}
else
{
if (mat[i][j] >= 4 )
is_balanced = false ;
}
}
}
if (is_balanced)
return "Balanced" ;
else
return "Unbalanced" ;
}
public static void main(String[] args)
{
int mat[][] = {{ 1 , 2 , 3 , 4 },
{ 3 , 5 , 2 , 6 },
{ 5 , 3 , 6 , 1 },
{ 9 , 5 , 6 , 0 }};
System.out.print(balancedMatrix(mat));
}
}
|
Python3
N = 4
M = 4
def balancedMatrix(mat):
is_balanced = True
i = 0
while i < N and is_balanced:
j = 0
while j < N and is_balanced:
if ((i = = 0 or i = = N - 1 ) and
(j = = 0 or j = = M - 1 )):
if mat[i][j] > = 2 :
isbalanced = False
elif (i = = 0 or i = = N - 1 or
j = = 0 or j = = M - 1 ):
if mat[i][j] > = 3 :
is_balanced = False
else :
if mat[i][j] > = 4 :
is_balanced = False
j + = 1
i + = 1
if is_balanced:
return "Balanced"
else :
return "Unbalanced"
mat = [ [ 1 , 2 , 3 , 4 ],
[ 3 , 5 , 2 , 6 ],
[ 5 , 3 , 6 , 1 ],
[ 9 , 5 , 6 , 0 ] ]
print (balancedMatrix(mat))
|
C#
using System;
class GFG{
static readonly int N = 4;
static readonly int M = 4;
static String balancedMatrix( int [, ]mat)
{
bool is_balanced = true ;
for ( int i = 0; i < N && is_balanced; i++)
{
for ( int j = 0; j < M && is_balanced; j++)
{
if ((i == 0 || i == N - 1) &&
(j == 0 || j == M - 1))
{
if (mat[i, j] >= 2)
is_balanced = false ;
}
else if (i == 0 || i == N - 1 ||
j == 0 || j == M - 1)
{
if (mat[i, j] >= 3)
is_balanced = false ;
}
else
{
if (mat[i, j] >= 4)
is_balanced = false ;
}
}
}
if (is_balanced)
return "Balanced" ;
else
return "Unbalanced" ;
}
public static void Main(String[] args)
{
int [, ]mat = {{1, 2, 3, 4},
{3, 5, 2, 6},
{5, 3, 6, 1},
{9, 5, 6, 0}};
Console.Write(balancedMatrix(mat));
}
}
|
Javascript
<script>
let N = 4;
let M = 4;
function balancedMatrix(mat)
{
let is_balanced = true ;
for (let i = 0; i < N && is_balanced; i++)
{
for (let j = 0; j < M && is_balanced; j++)
{
if ((i == 0 || i == N - 1) &&
(j == 0 || j == M - 1))
{
if (mat[i][j] >= 2)
is_balanced = false ;
}
else if (i == 0 || i == N - 1 ||
j == 0 || j == M - 1)
{
if (mat[i][j] >= 3)
is_balanced = false ;
}
else
{
if (mat[i][j] >= 4)
is_balanced = false ;
}
}
}
if (is_balanced)
return "Balanced" ;
else
return "Unbalanced" ;
}
let mat = [[1, 2, 3, 4],
[3, 5, 2, 6],
[5, 3, 6, 1],
[9, 5, 6, 0]];
document.write(balancedMatrix(mat));
</script>
|
Time Complexity: O(N*M)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...