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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Define size of matrix
#define N 4
#define M 4
// Function to check given matrix
// balanced or unbalanced
string balancedMatrix(int mat[][M])
{
// Flag for check matrix is balanced
// or unbalanced
bool is_balanced = true;
// Iterate row until condition is true
for (int i = 0; i < N && is_balanced; i++) {
// Iterate cols until condition is true
for (int j = 0; j < M && is_balanced; j++) {
// Check for corner edge elements
if ((i == 0 || i == N - 1)
&& (j == 0 || j == M - 1)) {
if (mat[i][j] >= 2)
is_balanced = false;
}
// Check for border elements
else if (i == 0 || i == N - 1
|| j == 0 || j == M - 1) {
if (mat[i][j] >= 3)
is_balanced = false;
}
else {
// Check for the middle ones
if (mat[i][j] >= 4)
is_balanced = false;
}
}
}
// Return balanced or not
if (is_balanced)
return "Balanced";
else
return "Unbalanced";
}
// Driver Code
int main()
{
// Given Matrix mat[][]
int mat[N][M] = { { 1, 2, 3, 4 },
{ 3, 5, 2, 6 },
{ 5, 3, 6, 1 },
{ 9, 5, 6, 0 } };
// Function Call
cout << balancedMatrix(mat);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Define size of matrix
static final int N = 4;
static final int M = 4;
// Function to check given matrix
// balanced or unbalanced
static String balancedMatrix(int mat[][])
{
// Flag for check matrix is balanced
// or unbalanced
boolean is_balanced = true;
// Iterate row until condition is true
for (int i = 0; i < N && is_balanced; i++)
{
// Iterate cols until condition is true
for (int j = 0; j < M && is_balanced; j++)
{
// Check for corner edge elements
if ((i == 0 || i == N - 1) &&
(j == 0 || j == M - 1))
{
if (mat[i][j] >= 2)
is_balanced = false;
}
// Check for border elements
else if (i == 0 || i == N - 1 ||
j == 0 || j == M - 1)
{
if (mat[i][j] >= 3)
is_balanced = false;
}
else
{
// Check for the middle ones
if (mat[i][j] >= 4)
is_balanced = false;
}
}
}
// Return balanced or not
if (is_balanced)
return "Balanced";
else
return "Unbalanced";
}
// Driver Code
public static void main(String[] args)
{
// Given Matrix mat[][]
int mat[][] = {{1, 2, 3, 4},
{3, 5, 2, 6},
{5, 3, 6, 1},
{9, 5, 6, 0}};
// Function Call
System.out.print(balancedMatrix(mat));
}
}
// This code is contributed by shikhasingrajput
Python3
# Python3 program for the above approach
# Define the size of the matrix
N = 4
M = 4
# Function to check given matrix
# balanced or unbalanced
def balancedMatrix(mat):
# Flag for check matrix is balanced
# or unbalanced
is_balanced = True
# Iterate row until condition is true
i = 0
while i < N and is_balanced:
# Iterate cols until condition is true
j = 0
while j < N and is_balanced:
# Check for corner edge elements
if ((i == 0 or i == N - 1) and
(j == 0 or j == M - 1)):
if mat[i][j] >= 2:
isbalanced = False
# Check for border elements
elif (i == 0 or i == N - 1 or
j == 0 or j == M - 1):
if mat[i][j] >= 3:
is_balanced = False
# Check for the middle ones
else:
if mat[i][j] >= 4:
is_balanced = False
j += 1
i += 1
# Return balanced or not
if is_balanced:
return "Balanced"
else:
return "Unbalanced"
# Driver code
# Given matrix mat[][]
mat = [ [ 1, 2, 3, 4 ],
[ 3, 5, 2, 6 ],
[ 5, 3, 6, 1 ],
[ 9, 5, 6, 0 ] ]
# Function call
print(balancedMatrix(mat))
# This code is contributed by Stuti Pathak
C#
// C# program for the above approach
using System;
class GFG{
// Define size of matrix
static readonly int N = 4;
static readonly int M = 4;
// Function to check given matrix
// balanced or unbalanced
static String balancedMatrix(int [, ]mat)
{
// Flag for check matrix is balanced
// or unbalanced
bool is_balanced = true;
// Iterate row until condition is true
for (int i = 0; i < N && is_balanced; i++)
{
// Iterate cols until condition is true
for (int j = 0; j < M && is_balanced; j++)
{
// Check for corner edge elements
if ((i == 0 || i == N - 1) &&
(j == 0 || j == M - 1))
{
if (mat[i, j] >= 2)
is_balanced = false;
}
// Check for border elements
else if (i == 0 || i == N - 1 ||
j == 0 || j == M - 1)
{
if (mat[i, j] >= 3)
is_balanced = false;
}
else
{
// Check for the middle ones
if (mat[i, j] >= 4)
is_balanced = false;
}
}
}
// Return balanced or not
if (is_balanced)
return "Balanced";
else
return "Unbalanced";
}
// Driver Code
public static void Main(String[] args)
{
// Given Matrix [,]mat
int [, ]mat = {{1, 2, 3, 4},
{3, 5, 2, 6},
{5, 3, 6, 1},
{9, 5, 6, 0}};
// Function Call
Console.Write(balancedMatrix(mat));
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// Javascript Script program to implement
// the above approach
// Define size of matrix
let N = 4;
let M = 4;
// Function to check given matrix
// balanced or unbalanced
function balancedMatrix(mat)
{
// Flag for check matrix is balanced
// or unbalanced
let is_balanced = true;
// Iterate row until condition is true
for (let i = 0; i < N && is_balanced; i++)
{
// Iterate cols until condition is true
for (let j = 0; j < M && is_balanced; j++)
{
// Check for corner edge elements
if ((i == 0 || i == N - 1) &&
(j == 0 || j == M - 1))
{
if (mat[i][j] >= 2)
is_balanced = false;
}
// Check for border elements
else if (i == 0 || i == N - 1 ||
j == 0 || j == M - 1)
{
if (mat[i][j] >= 3)
is_balanced = false;
}
else
{
// Check for the middle ones
if (mat[i][j] >= 4)
is_balanced = false;
}
}
}
// Return balanced or not
if (is_balanced)
return "Balanced";
else
return "Unbalanced";
}
// Driver Code
// Given Matrix mat[][]
let mat = [[1, 2, 3, 4],
[3, 5, 2, 6],
[5, 3, 6, 1],
[9, 5, 6, 0]];
// Function Call
document.write(balancedMatrix(mat));
</script>
Time Complexity: O(N*M)
Auxiliary Space: O(1)
Similar Reads
Check if a given matrix is sparse or not A matrix is a two-dimensional data object having m rows and n columns, therefore a total of m*n values. If most of the values of a matrix are 0 then we say that the matrix is sparse. Consider a definition of Sparse where a matrix is considered sparse if the number of 0s is more than half of the elem
5 min read
Check if a Matrix is Reverse Bitonic or Not Given a matrix m[][], the task is to check if the given matrix is Reverse Bitonic or not. If the given matrix is Reverse Bitonic, then print Yes. Otherwise, print No. If all the rows and the columns of the given matrix have elements in one of the following orders: Strictly increasingStrictly decreas
9 min read
Check given matrix is magic square or not Given a matrix, check whether it's Magic Square or not. A Magic Square is a n x n matrix of the distinct elements from 1 to n2 where the sum of any row, column, or diagonal is always equal to the same number. Examples: Input : n = 3 2 7 6 9 5 1 4 3 8 Output : Magic matrix Explanation:In matrix sum o
9 min read
Check if a given matrix is Hankel or not Given a matrix m[][] of size n x n. The task is to check whether given matrix is Hankel Matrix or not.In linear algebra, a Hankel matrix (or catalecticant matrix), named after Hermann Hankel, is a square matrix in which each ascending skew-diagonal from left to right is constant. Examples: Input: n
7 min read
Check if a Matrix is Bitonic or not Given a matrix m[][], the task is to check if the given matrix is Bitonic or not. If the given matrix is Bitonic, then print YES. Otherwise, print NO. If all the rows and the columns of the given matrix have elements in one of the following orders: Strictly increasingStrictly decreasingStrictly incr
8 min read
Check if the Matrix follows the given constraints or not Given a matrix A[][] of size N*M, the task is to check if the given matrix satisfies the following two conditions or not: Sum of all elements is primeElement A[i][j] from the matrix should also be a prime if (i + j) is a prime. Examples: Input: N = 4, M = 5 A[][] = {{ 1, 2, 3, 2, 2 }, { 2, 2, 7, 7,
15+ min read