Find sum of all elements in a matrix except the elements in row and / or column of given cell ? | Python3 implementation of the approach A structure to represent a cell index ; r is row , varies from 0 to R - 1 ; c is column , varies from 0 to C - 1 ; Compute sum of all elements , sum of every row and sum every column ; Compute the desired sum for all given cell indexes ; Driver Code
class Cell : NEW_LINE INDENT def __init__ ( self , r , c ) : NEW_LINE INDENT self . r = r NEW_LINE self . c = c NEW_LINE DEDENT DEDENT def printSums ( mat , arr , n ) : NEW_LINE INDENT Sum = 0 NEW_LINE row , col = [ 0 ] * R , [ 0 ] * C NEW_LINE for i in range ( 0 , R ) : NEW_LINE INDENT for j in range ( 0 , C ) : NEW_LINE INDENT Sum += mat [ i ] [ j ] NEW_LINE row [ i ] += mat [ i ] [ j ] NEW_LINE col [ j ] += mat [ i ] [ j ] NEW_LINE DEDENT DEDENT for i in range ( 0 , n ) : NEW_LINE INDENT r0 , c0 = arr [ i ] . r , arr [ i ] . c NEW_LINE print ( Sum - row [ r0 ] - col [ c0 ] + mat [ r0 ] [ c0 ] ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT mat = [ [ 1 , 1 , 2 ] , [ 3 , 4 , 6 ] , [ 5 , 3 , 2 ] ] NEW_LINE R = C = 3 NEW_LINE arr = [ Cell ( 0 , 0 ) , Cell ( 1 , 1 ) , Cell ( 0 , 1 ) ] NEW_LINE n = len ( arr ) NEW_LINE printSums ( mat , arr , n ) NEW_LINE DEDENT