Find sum of all elements in a matrix except the elements in row and / or column of given cell ? | Java 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 ; 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
class GFG { static int R = 3 ; static int C = 3 ; static class Cell { int r ; int c ; public Cell ( int r , int c ) { this . r = r ; this . c = c ; } } ; static void printSums ( int mat [ ] [ ] , Cell arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int sum = 0 , r = arr [ i ] . r , c = arr [ i ] . c ; for ( int j = 0 ; j < R ; j ++ ) { for ( int k = 0 ; k < C ; k ++ ) { if ( j != r && k != c ) { sum += mat [ j ] [ k ] ; } } } System . out . println ( sum ) ; } } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 1 , 2 } , { 3 , 4 , 6 } , { 5 , 3 , 2 } } ; Cell arr [ ] = { new Cell ( 0 , 0 ) , new Cell ( 1 , 1 ) , new Cell ( 0 , 1 ) } ; int n = arr . length ; printSums ( mat , arr , n ) ; } }