Open In App

Check whether the given Matrix is balanced or not

Last Updated : 30 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. Traverse the given matrix mat[][].
  2. 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.
  3. 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.
  4. 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.
  5. 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>


Output: 

Unbalanced

 

Time Complexity: O(N*M) 
Auxiliary Space: O(1) 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads