Given a n*m matrix find the sum of the elements of the matrix without the element specified by its position
Examples:
Input : mat[] = {{1 2 4},
{5 6 8}}
cell = (0 2)
Output :22
We need to find sum of all elements
except mat[0][2].
Input : mat[][] = {{5 6 2 3}, {2 3 1 8}, {9 6 5 2}}
cell = (1 2)
Output :51
A simple solution is to traverse through matrix. For every visited cell, check if it is the cell to be ignored.
C++
#include<bits/stdc++.h>
using namespace std;
# define R 2
# define C 3
int calcSum(int arr[R][C],
int x, int y)
{
int sum = 0;
for (int i = 0; i < R; i++)
{
for (int j = 0; j < C; j++)
{
if (i != x || j != y)
sum = sum + arr[i][j];
}
}
return sum;
}
int main()
{
int x = 0, y = 2;
int arr[R][C] = {1, 2, 4,
5, 6, 8 };
cout << calcSum(arr, x, y);
}
|
Java
class Matrix {
static int calcSum(int[][] arr, int x, int y)
{
int sum = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (i != x || j != y)
sum = sum + arr[i][j];
}
}
return sum;
}
public static void main(String[] args)
{
int x = 0, y = 2;
int[][] arr = {
{ 1, 2, 4 }, { 5, 6, 8 },
};
System.out.println(calcSum(arr, x, y));
}
}
|
C#
using System;
class GFG
{
static int calcSum(int[,] arr,
int x, int y)
{
int sum = 0;
for (int i = 0;
i < arr.GetLength(0); i++)
{
for (int j = 0;
j < arr.GetLength(1); j++)
{
if (i != x || j != y)
sum = sum + arr[i, j];
}
}
return sum;
}
public static void Main()
{
int x = 0, y = 2;
int[,] arr = {{ 1, 2, 4 },
{ 5, 6, 8 }};
Console.Write(calcSum(arr, x, y));
}
}
|
Python 3
def calcSum(arr, x, y):
s = 0
for i in range(R):
for j in range(C):
if (i != x or j != y):
s = s + arr[i][j];
return s;
x = 0
y = 2
arr = [[ 1, 2, 4 ],
[ 5, 6, 8 ]]
R = 2
C = 3
print(calcSum(arr, x, y))
|
PHP
<?php
$R = 2;
$C = 3;
function calcSum(&$arr, $x, $y)
{
global $R,$C;
$sum = 0;
for ($i = 0; $i < $R; $i++)
{
for ($j = 0; $j < $C; $j++)
{
if ($i != $x || $j != $y)
$sum = $sum + $arr[$i][$j];
}
}
return $sum;
}
$x = 0;
$y = 2;
$arr = array(array(1, 2, 4),
array(5, 6, 8));
echo (calcSum($arr, $x, $y));
?>
|
Javascript
<script>
function calcSum(arr,x,y)
{
let sum = 0;
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[i].length; j++) {
if (i != x || j != y)
sum = sum + arr[i][j];
}
}
return sum;
}
let x = 0, y = 2;
let arr=[[1, 2, 4],[5, 6, 8 ]];
document.write(calcSum(arr, x, y));
</script>
|
The above solution causes an extra comparison for every matrix item. A better solution is to first find overall sum, then subtract given cell.
C++
#include<bits/stdc++.h>
using namespace std;
#define R 2
#define C 3
int calcSum(int arr[R][C],
int x, int y)
{
int sum = 0;
for (int i = 0; i < R; i++)
for (int j = 0; j < C; j++)
sum = sum + arr[i][j];
return sum - arr[x][y];
}
int main()
{
int x = 0, y = 2;
int arr[R][C]= {1, 2, 4 ,
5, 6, 8 };
cout << (calcSum(arr, x, y));
}
|
Java
class Matrix {
static int calcSum(int[][] arr, int x, int y)
{
int sum = 0;
for (int i = 0; i < arr.length; i++)
for (int j = 0; j < arr[i].length; j++)
sum = sum + arr[i][j];
return sum - arr[x][y];
}
public static void main(String[] args)
{
int x = 0, y = 2;
int[][] arr = {
{ 1, 2, 4 }, { 5, 6, 8 },
};
System.out.println(calcSum(arr, x, y));
}
}
|
C#
using System;
class GFG
{
static int calcSum(int[,] arr,
int x, int y)
{
int sum = 0;
for (int i = 0;
i < arr.GetLength(0); i++)
for (int j = 0;
j < arr.GetLength(1); j++)
sum = sum + arr[i, j];
return sum - arr[x, y];
}
public static void Main()
{
int x = 0, y = 2;
int[,] arr = {{ 1, 2, 4 },
{ 5, 6, 8 }};
Console.Write(calcSum(arr, x, y));
}
}
|
Python 3
def calcSum( arr, x, y):
s = 0
for i in range(R):
for j in range(C):
s = s + arr[i][j]
return s - arr[x][y]
x = 0
y = 2
R = 2
C = 3
arr = [[1, 2, 4 ],
[5, 6, 8 ]]
print (calcSum(arr, x, y))
|
PHP
<?php
$R = 2;
$C = 3;
function calcSum(&$arr, $x, $y)
{
global $R,$C;
$sum = 0;
for ($i = 0; $i < $R; $i++)
for ($j = 0; $j < $C; $j++)
$sum = $sum + $arr[$i][$j];
return $sum - $arr[$x][$y];
}
$x = 0;
$y = 2;
$arr= array(array(1, 2, 4 ),
array(5, 6, 8 ));
echo (calcSum($arr, $x, $y));
?>
|
Javascript
<script>
function calcSum(arr,x,y)
{
let sum = 0;
for (let i = 0; i < arr.length; i++)
for (let j = 0; j < arr[i].length; j++)
sum = sum + arr[i][j];
return sum - arr[x][y];
}
let x = 0, y = 2;
let arr = [[1, 2, 4 ],
[5, 6, 8 ]];
document.write(calcSum(arr, x, y));
</script>
|
Attention reader! All those who say programming isn’t for kids, just haven’t met the right mentors yet. Join the Demo Class for First Step to Coding Course, specifically designed for students of class 8 to 12.
The students will get to learn more about the world of programming in these free classes which will definitely help them in making a wise career choice in the future.