Find sum of all elements in a matrix except the elements in row and / or column of given cell ? | javascript implementation of the approach ; A structure to represent a cell index ; Constructor ; A simple solution to find sums for a given array of cell indexes ; Iterate through all cell indexes ; Compute sum for current cell index ; Driver code
var R = 3 ; var C = 3 ; class Cell { constructor ( r , c ) { this . r = r ; this . c = c ; } } function printSums ( mat , arr , n ) { for ( i = 0 ; i < n ; i ++ ) { var sum = 0 , r = arr [ i ] . r , c = arr [ i ] . c ; for ( j = 0 ; j < R ; j ++ ) { for ( k = 0 ; k < C ; k ++ ) { if ( j != r && k != c ) { sum += mat [ j ] [ k ] ; } } } document . write ( sum + " " ) ; } } var mat = [ [ 1 , 1 , 2 ] , [ 3 , 4 , 6 ] , [ 5 , 3 , 2 ] ] ; var arr = [ new Cell ( 0 , 0 ) , new Cell ( 1 , 1 ) , new Cell ( 0 , 1 ) ] ; var n = arr . length ; printSums ( mat , arr , n ) ;