Sum of all parts of a square Matrix divided by its diagonals

22 Jun, 2021
Given a 2D matrix arr[][] of N*N dimensions, the task is to find the sum of elements of all four parts of the matrix divided by the diagonals without including the diagonal elements in any of the four parts.Example:  
Input: arr[][] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} } Output: 2 4 6 8 Explanation: (1, 5, 9) and (3, 5, 7) are diagonals of arr[][] matrix. Therefore sum of parts are: Top = 2 Left = 4 Right = 6 Bottom = 8Input: arr[][] = { {1, 3, 1, 5}, {2, 2, 4, 1}, {5, 0, 2, 3}, { 1, 3, 3, 5} } Output: 4 7 4 6 Explanation: (1, 2, 2, 5) and (5, 4, 0, 1) are diagonals of arr[][] matrix. Therefore sum of parts are: Top = 3 + 1 = 4 Left = 2 + 5 = 7 Right = 1 + 3 = 4 Bottom = 3 + 3 = 6  
 
Approach:  
As shown in the above figure, After the matrix of size NxN is divided by diagonals. We observe the following properties:  
If sum of index of row and column is less than N – 1 then, it belongs to either Top part or Left part. If column index is greater than row index it belongs to Top part.Else it belongs to Left part.Else it belongs to either Right part or Down part.If column index is greater than row index it belongs to Right part.Else it belongs to Down part.
If sum of index of row and column is less than N – 1 then, it belongs to either Top part or Left part. If column index is greater than row index it belongs to Top part.Else it belongs to Left part.
If column index is greater than row index it belongs to Top part.
Else it belongs to Left part.
Else it belongs to either Right part or Down part.If column index is greater than row index it belongs to Right part.Else it belongs to Down part.
If column index is greater than row index it belongs to Right part.
Else it belongs to Down part.
Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ program for the above approach#include "bits/stdc++.h"using namespace std; // Function to calculate the// sum of all parts of matrixvoid SumOfPartsOfMetrics(int* arr,                         int N){     // To store the sum of all four    // parts of the diagonals    int top, bottom, left, right;     // Initialise respective sum    // as zero    top = bottom = right = left = 0;     // Traversing the matrix    for (int i = 0; i < N; i++) {        for (int j = 0; j < N; j++) {             // If i + j < N -1            // then it belongs to            // top or left            if (i + j < N - 1 && i != j) {                 // Belongs to top                if (i < j) {                    top += (arr + i * N)[j];                }                 // Belongs to left                else {                    left += (arr + i * N)[j];                }            }             // If i+j > N - 1 then            // it belongs to right            // or bottom            else if (i + j > N - 1 && i != j) {                 // Belongs to right                if (i > j) {                    bottom += (arr + i * N)[j];                }                 // Belongs to bottom                else {                    right += (arr + i * N)[j];                }            }        }    }    cout << top << ' ' << left         << ' ' << right << ' '         << bottom << endl;} // Driver Codeint main(){    int N = 4;    int arr[N][N] = { { 1, 3, 1, 5 },                      { 2, 2, 4, 1 },                      { 5, 0, 2, 3 },                      { 1, 3, 3, 5 } };    // Function call to find print    // sum of al parts    SumOfPartsOfMetrics((int*)arr, N);    return 0;}
// Java program for the above approachclass GFG { // Function to calculate the// sum of all parts of matrixstatic void SumOfPartsOfMetrics(int [][]arr,                                int N){         // To store the sum of all four    // parts of the diagonals    // Initialise respective sum    // as zero    int top = 0, bottom = 0;    int left = 0, right = 0;     // Traversing the matrix    for(int i = 0; i < N; i++)    {       for(int j = 0; j < N; j++)       {                     // If i + j < N -1          // then it belongs to          // top or left          if (i + j < N - 1 && i != j)          {                             // Belongs to top              if (i < j)              {                  top += arr[i][j];              }                             // Belongs to left              else              {                  left += arr[i][j];              }          }                     // If i+j > N - 1 then          // it belongs to right          // or bottom          else if (i + j > N - 1 && i != j)          {                             // Belongs to right              if (i > j)              {                  bottom += arr[i][j];              }                             // Belongs to bottom              else              {                  right += arr[i][j];              }          }       }    }    System.out.println(top + " " + left + " " +                     right + " " + bottom);}     // Driver Codepublic static void main (String[] args){    int N = 4;    int arr[][] = { { 1, 3, 1, 5 },                    { 2, 2, 4, 1 },                    { 5, 0, 2, 3 },                    { 1, 3, 3, 5 } };                             // Function call to find print    // sum of al parts    SumOfPartsOfMetrics(arr, N);}} // This code is contributed by AnkitRai01
# Python3 program for the above approach # Function to calculate the# sum of all parts of matrixdef SumOfPartsOfMetrics(arr, N):     # To store the sum of all four    # parts of the diagonals    # Initialise respective sum    # as zero    top = bottom = right = left = 0;     # Traversing the matrix    for i in range(N):        for j in range(N):             # If i + j < N -1            # then it belongs to            # top or left            if (i + j < N - 1 and i != j):                 # Belongs to top                if (i < j):                    top += arr[i][j];                             # Belongs to left                else:                    left += arr[i][j];             # If i+j > N - 1 then            # it belongs to right            # or bottom            elif (i + j > N - 1 and i != j):                 # Belongs to right                if (i > j):                    bottom += arr[i][j];                 # Belongs to bottom                else:                    right += arr[i][j];                 print(top, left, right, bottom); # Driver Codeif __name__ == "__main__":     N = 4;    arr = [ [ 1, 3, 1, 5 ],            [ 2, 2, 4, 1 ],            [ 5, 0, 2, 3 ],            [ 1, 3, 3, 5 ] ];                 # Function call to find print    # sum of al parts    SumOfPartsOfMetrics(arr, N);     # This code is contributed by AnkitRai01
// C# program for the above approachusing System; class GFG { // Function to calculate the// sum of all parts of matrixstatic void SumOfPartsOfMetrics(int [,]arr,                                int N){             // To store the sum of all four    // parts of the diagonals    // Initialise respective sum    // as zero    int top = 0, bottom = 0;    int left = 0, right = 0;         // Traversing the matrix    for(int i = 0; i < N; i++)    {       for(int j = 0; j < N; j++)       {           // If i + j < N -1          // then it belongs to          // top or left          if (i + j < N - 1 && i != j)          {                             // Belongs to top              if (i < j)              {                  top += arr[i, j];              }                             // Belongs to left              else              {                  left += arr[i, j];              }          }                     // If i+j > N - 1 then          // it belongs to right          // or bottom          else if (i + j > N - 1 && i != j)          {                             // Belongs to right            if (i > j)            {                bottom += arr[i, j];            }                         // Belongs to bottom            else            {                right += arr[i, j];            }          }       }    }    Console.WriteLine(top + " " + left + " " +                    right + " " + bottom);}         // Driver Codepublic static void Main (string[] args){    int N = 4;    int [,]arr = { { 1, 3, 1, 5 },                   { 2, 2, 4, 1 },                   { 5, 0, 2, 3 },                   { 1, 3, 3, 5 } };                                 // Function call to find print    // sum of al parts    SumOfPartsOfMetrics(arr, N);}} // This code is contributed by AnkitRai01
<script>// Javascript program for the above approach // Function to calculate the// sum of all parts of matrixfunction SumOfPartsOfMetrics(arr, N){     // To store the sum of all four    // parts of the diagonals    let top, bottom, left, right;     // Initialise respective sum    // as zero    top = bottom = right = left = 0;     // Traversing the matrix    for (let i = 0; i < N; i++) {        for (let j = 0; j < N; j++) {             // If i + j < N -1            // then it belongs to            // top or left            if (i + j < N - 1 && i != j) {                 // Belongs to top                if (i < j) {                    top += arr[i][j];                }                 // Belongs to left                else {                    left += arr[i][j];                }            }             // If i+j > N - 1 then            // it belongs to right            // or bottom            else if (i + j > N - 1 && i != j) {                 // Belongs to right                if (i > j) {                    bottom += arr[i][j];                }                 // Belongs to bottom                else {                    right += arr[i][j];                }            }        }    }    document.write(top + ' ' + left         + ' ' + right + ' '         + bottom + "<br>");} // Driver Code    let N = 4;    let arr = [ [ 1, 3, 1, 5 ],                      [ 2, 2, 4, 1 ],                      [ 5, 0, 2, 3 ],                      [ 1, 3, 3, 5 ] ];    // Function call to find print    // sum of al parts    SumOfPartsOfMetrics(arr, N); // This code is contributed by rishavmahato348.</script>
4 7 4 6
 
Time Complexity: O(N2) 
ankthon
rishavmahato348
adnanirshad158
Competitive Programming
Matrix
Matrix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.