Check whether the given Matrix is balanced or not | Define size of 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
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 " " ; else return " " ; } let mat = [ [ 1 , 2 , 3 , 4 ] , [ 3 , 5 , 2 , 6 ] , [ 5 , 3 , 6 , 1 ] , [ 9 , 5 , 6 , 0 ] ] ; document . write ( balancedMatrix ( mat ) ) ;