Find sum of all Boundary and Diagonal element of a Matrix - GeeksforGeeks

14 Dec, 2021
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.
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++
Java
Python3
C#
Javascript
// C++ implementation of the above approach #include "bits/stdc++.h"using namespace std; const int N = 4; // Function to find the sum of all diagonal// and Boundary elementsvoid diagonalBoundarySum(int arr[N][N]){     int requiredSum = 0;     // Traverse arr[][]    // Loop from i to N-1 for rows    for (int i = 0; i < N; i++) {         // Loop from j = N-1 for columns        for (int j = 0; j < N; j++) {             // Condition for diagonal            // elements            if (i == j || (i + j) == N - 1) {                requiredSum += arr[i][j];            }             // Condition for Boundary            // elements            else if (i == 0 || j == 0                     || i == N - 1                     || j == N - 1) {                requiredSum += arr[i][j];            }        }    }     // Print the final Sum    cout << requiredSum << endl;} // Driver Codeint 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 implementation of the above approachimport java.util.*; class GFG{    public static int N = 4;         // Function to find the sum of all diagonal    // and Boundary elements    static void diagonalBoundarySum(int arr[][]){        int requiredSum = 0;                 // Traverse arr[][]        // Loop from i to N-1 for rows        for (int i = 0; i < N; i++) {                 // Loop from j = N-1 for columns            for (int j = 0; j < N; j++) {                     // Condition for diagonal                // elements                if (i == j || (i + j) == N - 1) {                    requiredSum += arr[i][j];                }                     // Condition for Boundary                // elements                else if (i == 0 || j == 0 || i == N - 1|| j == N - 1) {                    requiredSum += arr[i][j];                }            }        }             // Print the final Sum        System.out.println(requiredSum);    }         // Driver Code    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);             }} // This code is contributed by AbhiThakur
# Python implementation of the above approach N = 4; # Function to find the sum of all diagonal# and Boundary elementsdef diagonalBoundarySum(arr):    requiredSum = 0;     # Traverse arr    # Loop from i to N-1 for rows    for i in range(N):         # Loop from j = N-1 for columns        for j in range(N):             # Condition for diagonal            # elements            if (i == j or (i + j) == N - 1):                requiredSum += arr[i][j];                         # Condition for Boundary            # elements            elif(i == 0 or j == 0 or i == N - 1 or j == N - 1):                requiredSum += arr[i][j];     # Print the final Sum    print(requiredSum);  # Driver Codeif __name__ == '__main__':    arr = [[ 1, 2, 3, 4 ],    [ 1, 2, 3, 4 ],    [ 1, 2, 3, 4 ],    [ 1, 2, 3, 4 ]];     diagonalBoundarySum(arr); # This code is contributed by 29AjayKumar
// C# implementation of the above approachusing System; class GFG{    public static int N = 4;         // Function to find the sum of all diagonal    // and Boundary elements    static void diagonalBoundarySum(int[, ] arr){        int requiredSum = 0;                 // Traverse arr[][]        // Loop from i to N-1 for rows        for (int i = 0; i < N; i++) {                 // Loop from j = N-1 for columns            for (int j = 0; j < N; j++) {                     // Condition for diagonal                // elements                if (i == j || (i + j) == N - 1) {                    requiredSum += arr[i,j];                }                     // Condition for Boundary                // elements                else if (i == 0 || j == 0 || i == N - 1|| j == N - 1) {                    requiredSum += arr[i,j];                }            }        }             // Print the final Sum        Console.WriteLine(requiredSum);    }         // Driver Code    public static void Main()    {        int[, ] arr = { { 1, 2, 3, 4 },{ 1, 2, 3, 4 },{ 1, 2, 3, 4 },{ 1, 2, 3, 4 } };             diagonalBoundarySum(arr);             }} // This code is contributed by abhaysingh290895
<script>// Java script  implementation of the above approachlet N = 4;         // Function to find the sum of all diagonal    // and Boundary elements    function diagonalBoundarySum(arr){        let requiredSum = 0;                 // Traverse arr[][]        // Loop from i to N-1 for rows        for (let i = 0; i < N; i++) {                 // Loop from j = N-1 for columns            for (let j = 0; j < N; j++) {                     // Condition for diagonal                // elements                if (i == j || (i + j) == N - 1) {                    requiredSum += arr[i][j];                }                     // Condition for Boundary                // elements                else if (i == 0 || j == 0 || i == N - 1|| j == N - 1) {                    requiredSum += arr[i][j];                }            }        }             // Print the final Sum        document.write(requiredSum);    }         // Driver Code             let arr = [[ 1, 2, 3, 4 ],[ 1, 2, 3, 4 ],                        [1, 2, 3, 4 ],[ 1, 2, 3, 4 ]];             diagonalBoundarySum(arr);     // contributed by sravan kumar</script>
40
 
abhaysingh290895
29AjayKumar
sravankumar8128
ankita_saini
debojyoti0391
Arrays
Matrix
School Programming
Arrays
Matrix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Window Sliding Technique
Trapping Rain Water
Building Heap from Array
Reversal algorithm for array rotation
Program to find sum of elements in a given array
Matrix Chain Multiplication | DP-8
Rat in a Maze | Backtracking-2
Divide and Conquer | Set 5 (Strassen's Matrix Multiplication)
Sudoku | Backtracking-7
Find the number of islands | Set 1 (Using DFS)