Check whether the given Matrix is balanced or not | Define the size of the matrix ; Function to check given matrix balanced or unbalanced ; Flag for check matrix is balanced or unbalanced ; Iterate row until condition is true ; Iterate cols until condition is true ; Check for corner edge elements ; Check for border elements ; Check for the middle ones ; Return balanced or not ; Given matrix mat [ ] [ ] ; Function call
N = 4 NEW_LINE M = 4 NEW_LINE def balancedMatrix ( mat ) : NEW_LINE INDENT is_balanced = True NEW_LINE i = 0 NEW_LINE while i < N and is_balanced : NEW_LINE INDENT j = 0 NEW_LINE while j < N and is_balanced : NEW_LINE INDENT if ( ( i == 0 or i == N - 1 ) and ( j == 0 or j == M - 1 ) ) : NEW_LINE INDENT if mat [ i ] [ j ] >= 2 : NEW_LINE INDENT isbalanced = False NEW_LINE DEDENT DEDENT elif ( i == 0 or i == N - 1 or j == 0 or j == M - 1 ) : NEW_LINE INDENT if mat [ i ] [ j ] >= 3 : NEW_LINE INDENT is_balanced = False NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if mat [ i ] [ j ] >= 4 : NEW_LINE INDENT is_balanced = False NEW_LINE DEDENT DEDENT j += 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT if is_balanced : NEW_LINE INDENT return " Balanced " NEW_LINE DEDENT else : NEW_LINE INDENT return " Unbalanced " NEW_LINE DEDENT DEDENT mat = [ [ 1 , 2 , 3 , 4 ] , [ 3 , 5 , 2 , 6 ] , [ 5 , 3 , 6 , 1 ] , [ 9 , 5 , 6 , 0 ] ] NEW_LINE print ( balancedMatrix ( mat ) ) NEW_LINE