Find sum of all Boundary and Diagonal element of a Matrix
Last Updated :
13 Sep, 2022
Given a 2D array arr[][] of order NxN, the task is to find the sum of all the elements present in both the diagonals and boundary elements of the given arr[][].
Examples:
Input: arr[][] = { {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4} }
Output: 40
Explanation:
The Sum of elements on the boundary is 1 + 2 + 3 + 4 + 4 + 4 + 4 + 3 + 2 + 1 + 1 + 1 = 30.
The Sum of elements on the diagonals which do not intersect with the boundary elements is 2 + 3 + 2 + 3 = 10.
Therefore the required sum is 30 + 10 = 40.
Input: arr[][] = { {1, 2, 3}, {1, 2, 3}, {1, 2, 3}}
Output: 18
Explanation:
The Sum of elements on the boundary is 1 + 2 + 3 + 3 + 3 + 2 + 1 + 1 = 16.
The Sum of elements on the diagonals which do not intersect with the boundary elements is 2.
Therefore the required sum is 16 + 2 = 18.
Approach:
- Traverse the given 2D array with two loops, one for rows(say i) and another for columns(say j).
- If i equals to j or (i + j) equals to (size of column – 1) then that element contributes to diagonals of the given 2D array.
- If (i or j equals to 0) or (i or j equals to size of column – 1) then that element contributes to boundary elements of the given 2D array.
- The sum of all the element satisfying above two conditions gives the required sum.
Below is the implementation of the above approach:
C++
#include "bits/stdc++.h"
using namespace std;
const int N = 4;
void diagonalBoundarySum( int arr[N][N])
{
int requiredSum = 0;
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < N; j++) {
if (i == j || (i + j) == N - 1) {
requiredSum += arr[i][j];
}
else if (i == 0 || j == 0
|| i == N - 1
|| j == N - 1) {
requiredSum += arr[i][j];
}
}
}
cout << requiredSum << endl;
}
int main()
{
int arr[][4] = { { 1, 2, 3, 4 },
{ 1, 2, 3, 4 },
{ 1, 2, 3, 4 },
{ 1, 2, 3, 4 } };
diagonalBoundarySum(arr);
return 0;
}
|
Java
import java.util.*;
class GFG{
public static int N = 4 ;
static void diagonalBoundarySum( int arr[][]){
int requiredSum = 0 ;
for ( int i = 0 ; i < N; i++) {
for ( int j = 0 ; j < N; j++) {
if (i == j || (i + j) == N - 1 ) {
requiredSum += arr[i][j];
}
else if (i == 0 || j == 0 || i == N - 1 || j == N - 1 ) {
requiredSum += arr[i][j];
}
}
}
System.out.println(requiredSum);
}
public static void main(String args[])
{
int arr[][] = { { 1 , 2 , 3 , 4 },{ 1 , 2 , 3 , 4 },
{ 1 , 2 , 3 , 4 },{ 1 , 2 , 3 , 4 } };
diagonalBoundarySum(arr);
}
}
|
Python3
N = 4 ;
def diagonalBoundarySum(arr):
requiredSum = 0 ;
for i in range (N):
for j in range (N):
if (i = = j or (i + j) = = N - 1 ):
requiredSum + = arr[i][j];
elif (i = = 0 or j = = 0 or i = = N - 1 or j = = N - 1 ):
requiredSum + = arr[i][j];
print (requiredSum);
if __name__ = = '__main__' :
arr = [[ 1 , 2 , 3 , 4 ],
[ 1 , 2 , 3 , 4 ],
[ 1 , 2 , 3 , 4 ],
[ 1 , 2 , 3 , 4 ]];
diagonalBoundarySum(arr);
|
C#
using System;
class GFG
{
public static int N = 4;
static void diagonalBoundarySum( int [, ] arr){
int requiredSum = 0;
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < N; j++) {
if (i == j || (i + j) == N - 1) {
requiredSum += arr[i,j];
}
else if (i == 0 || j == 0 || i == N - 1|| j == N - 1) {
requiredSum += arr[i,j];
}
}
}
Console.WriteLine(requiredSum);
}
public static void Main()
{
int [, ] arr = { { 1, 2, 3, 4 },{ 1, 2, 3, 4 },{ 1, 2, 3, 4 },{ 1, 2, 3, 4 } };
diagonalBoundarySum(arr);
}
}
|
Javascript
<script>
let N = 4;
function diagonalBoundarySum(arr){
let requiredSum = 0;
for (let i = 0; i < N; i++) {
for (let j = 0; j < N; j++) {
if (i == j || (i + j) == N - 1) {
requiredSum += arr[i][j];
}
else if (i == 0 || j == 0 || i == N - 1|| j == N - 1) {
requiredSum += arr[i][j];
}
}
}
document.write(requiredSum);
}
let arr = [[ 1, 2, 3, 4 ],[ 1, 2, 3, 4 ],
[1, 2, 3, 4 ],[ 1, 2, 3, 4 ]];
diagonalBoundarySum(arr);
</script>
|
Time complexity: O(N*N) for given array of N*N size
Auxiliary space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...