Given a 2D matrix and a set of cell indexes e.g., an array of (i, j) where i indicates row and j column. For every given cell index (i, j), find sums of all matrix elements except the elements present in i’th row and/or j’th column.
Example:
mat[][] = { {1, 1, 2}
{3, 4, 6}
{5, 3, 2} }
Array of Cell Indexes: {(0, 0), (1, 1), (0, 1)}
Output: 15, 10, 16
A Naive Solution is to one by once consider all given cell indexes. For every cell index (i, j), find the sum of matrix elements that are not present either at i’th row or at j’th column.
Implementation:
C++
#include<bits/stdc++.h>
#define R 3
#define C 3
using namespace std;
struct Cell
{
int r;
int c;
};
void printSums( int mat[][C], struct 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];
cout << sum << endl;
}
}
int main()
{
int mat[][C] = {{1, 1, 2}, {3, 4, 6}, {5, 3, 2}};
struct Cell arr[] = {{0, 0}, {1, 1}, {0, 1}};
int n = sizeof (arr)/ sizeof (arr[0]);
printSums(mat, arr, n);
return 0;
}
|
Java
import java.io.*;
public 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);
}
}
|
Python3
class Cell:
def __init__( self , r, c):
self .r = r
self .c = c
def printSums(mat, arr, n):
for i in range ( 0 , n):
Sum = 0 ; r = arr[i].r; c = arr[i].c
for j in range ( 0 , R):
for k in range ( 0 , C):
if j ! = r and k ! = c:
Sum + = mat[j][k]
print ( Sum )
if __name__ = = "__main__" :
mat = [[ 1 , 1 , 2 ], [ 3 , 4 , 6 ], [ 5 , 3 , 2 ]]
R = C = 3
arr = [Cell( 0 , 0 ), Cell( 1 , 1 ), Cell( 0 , 1 )]
n = len (arr)
printSums(mat, arr, n)
|
C#
using System;
class GFG
{
static int R = 3;
static int C = 3;
public class Cell
{
public int r;
public 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];
}
}
}
Console.WriteLine(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);
}
}
|
Javascript
<script>
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+ "<br/>" );
}
}
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);
</script>
|
Time Complexity of the above solution is O(n * R * C) where n is number of given cell indexes and R x C is matrix size.
Auxiliary Space: O(1)
An Efficient Solution can compute all sums in O(R x C + n) time. The idea is to precompute total sum, row and column sums before processing the given array of indexes.
- Calculate sum of matrix, call it sum.
- Calculate sum of individual rows and columns. (row[] and col[])
- For a cell index (i, j), the desired sum will be “sum- row[i] – col[j] + arr[i][j]”
Below is the implementation of above idea.
C++
#include<bits/stdc++.h>
#define R 3
#define C 3
using namespace std;
struct Cell
{
int r;
int c;
};
void printSums( int mat[][C], struct Cell arr[], int n)
{
int sum = 0;
int row[R] = {};
int col[C] = {};
for ( int i=0; i<R; i++)
{
for ( int j=0; j<C; j++)
{
sum += mat[i][j];
col[j] += mat[i][j];
row[i] += mat[i][j];
}
}
for ( int i=0; i<n; i++)
{
int ro = arr[i].r, co = arr[i].c;
cout << sum - row[ro] - col[co] + mat[ro][co] << endl;
}
}
int main()
{
int mat[][C] = {{1, 1, 2}, {3, 4, 6}, {5, 3, 2}};
struct Cell arr[] = {{0, 0}, {1, 1}, {0, 1}};
int n = sizeof (arr)/ sizeof (arr[0]);
printSums(mat, arr, n);
return 0;
}
|
Java
import java.io.*;
public 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)
{
int sum = 0 ;
int []row = new int [R];
int []col = new int [C];
for ( int i = 0 ; i < R; i++)
{
for ( int j = 0 ; j < C; j++)
{
sum += mat[i][j];
col[j] += mat[i][j];
row[i] += mat[i][j];
}
}
for ( int i = 0 ; i < n; i++)
{
int ro = arr[i].r, co = arr[i].c;
System.out.println(sum - row[ro] - col[co] +
mat[ro][co]);
}
}
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);
}
}
|
Python3
class Cell:
def __init__( self , r, c):
self .r = r
self .c = c
def printSums(mat, arr, n):
Sum = 0
row, col = [ 0 ] * R, [ 0 ] * C
for i in range ( 0 , R):
for j in range ( 0 , C):
Sum + = mat[i][j]
row[i] + = mat[i][j]
col[j] + = mat[i][j]
for i in range ( 0 , n):
r0, c0 = arr[i].r, arr[i].c
print ( Sum - row[r0] - col[c0] + mat[r0][c0])
if __name__ = = "__main__" :
mat = [[ 1 , 1 , 2 ], [ 3 , 4 , 6 ], [ 5 , 3 , 2 ]]
R = C = 3
arr = [Cell( 0 , 0 ), Cell( 1 , 1 ), Cell( 0 , 1 )]
n = len (arr)
printSums(mat, arr, n)
|
C#
using System;
class GFG
{
static int R = 3;
static int C = 3;
public class Cell
{
public int r;
public int c;
public Cell( int r, int c)
{
this .r = r;
this .c = c;
}
};
static void printSums( int [,]mat,
Cell []arr, int n)
{
int sum = 0;
int []row = new int [R];
int []col = new int [C];
for ( int i = 0; i < R; i++)
{
for ( int j = 0; j < C; j++)
{
sum += mat[i, j];
col[j] += mat[i, j];
row[i] += mat[i, j];
}
}
for ( int i = 0; i < n; i++)
{
int ro = arr[i].r, co = arr[i].c;
Console.WriteLine(sum - row[ro] - col[co] +
mat[ro, co]);
}
}
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);
}
}
|
Javascript
<script>
var R = 3;
var C = 3;
class Cell
{
constructor(r, c)
{
this .r = r;
this .c = c;
}
};
function printSums(mat, arr, n)
{
var sum = 0;
var row = Array(R).fill(0);
var col = Array(C).fill(0);
for ( var i = 0; i < R; i++)
{
for ( var j = 0; j < C; j++)
{
sum += mat[i][j];
col[j] += mat[i][j];
row[i] += mat[i][j];
}
}
for ( var i = 0; i < n; i++)
{
var ro = arr[i].r, co = arr[i].c;
document.write(sum - row[ro] - col[co] +
mat[ro][co] + "<br>" );
}
}
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);
</script>
|
Time Complexity: O(R x C + n)
Auxiliary Space: O(R + C)
Thanks to Gaurav Ahirwar for suggesting this efficient solution.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
06 Oct, 2023
Like Article
Save Article