Sum of all parts of a square Matrix divided by its diagonals
Last Updated :
31 Aug, 2022
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 = 8
Input: 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.
Below is the implementation of the above approach:
C++
#include "bits/stdc++.h"
using namespace std;
void SumOfPartsOfMetrics( int * arr,
int N)
{
int top, bottom, left, right;
top = bottom = right = left = 0;
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < N; j++) {
if (i + j < N - 1 && i != j) {
if (i < j) {
top += (arr + i * N)[j];
}
else {
left += (arr + i * N)[j];
}
}
else if (i + j > N - 1 && i != j) {
if (i > j) {
bottom += (arr + i * N)[j];
}
else {
right += (arr + i * N)[j];
}
}
}
}
cout << top << ' ' << left
<< ' ' << right << ' '
<< bottom << endl;
}
int main()
{
int N = 4;
int arr[N][N] = { { 1, 3, 1, 5 },
{ 2, 2, 4, 1 },
{ 5, 0, 2, 3 },
{ 1, 3, 3, 5 } };
SumOfPartsOfMetrics(( int *)arr, N);
return 0;
}
|
Java
class GFG {
static void SumOfPartsOfMetrics( int [][]arr,
int N)
{
int top = 0 , bottom = 0 ;
int left = 0 , right = 0 ;
for ( int i = 0 ; i < N; i++)
{
for ( int j = 0 ; j < N; j++)
{
if (i + j < N - 1 && i != j)
{
if (i < j)
{
top += arr[i][j];
}
else
{
left += arr[i][j];
}
}
else if (i + j > N - 1 && i != j)
{
if (i > j)
{
bottom += arr[i][j];
}
else
{
right += arr[i][j];
}
}
}
}
System.out.println(top + " " + left + " " +
right + " " + bottom);
}
public 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 } };
SumOfPartsOfMetrics(arr, N);
}
}
|
Python3
def SumOfPartsOfMetrics(arr, N):
top = bottom = right = left = 0 ;
for i in range (N):
for j in range (N):
if (i + j < N - 1 and i ! = j):
if (i < j):
top + = arr[i][j];
else :
left + = arr[i][j];
elif (i + j > N - 1 and i ! = j):
if (i > j):
bottom + = arr[i][j];
else :
right + = arr[i][j];
print (top, left, right, bottom);
if __name__ = = "__main__" :
N = 4 ;
arr = [ [ 1 , 3 , 1 , 5 ],
[ 2 , 2 , 4 , 1 ],
[ 5 , 0 , 2 , 3 ],
[ 1 , 3 , 3 , 5 ] ];
SumOfPartsOfMetrics(arr, N);
|
C#
using System;
class GFG {
static void SumOfPartsOfMetrics( int [,]arr,
int N)
{
int top = 0, bottom = 0;
int left = 0, right = 0;
for ( int i = 0; i < N; i++)
{
for ( int j = 0; j < N; j++)
{
if (i + j < N - 1 && i != j)
{
if (i < j)
{
top += arr[i, j];
}
else
{
left += arr[i, j];
}
}
else if (i + j > N - 1 && i != j)
{
if (i > j)
{
bottom += arr[i, j];
}
else
{
right += arr[i, j];
}
}
}
}
Console.WriteLine(top + " " + left + " " +
right + " " + bottom);
}
public 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 } };
SumOfPartsOfMetrics(arr, N);
}
}
|
Javascript
<script>
function SumOfPartsOfMetrics(arr, N)
{
let top, bottom, left, right;
top = bottom = right = left = 0;
for (let i = 0; i < N; i++) {
for (let j = 0; j < N; j++) {
if (i + j < N - 1 && i != j) {
if (i < j) {
top += arr[i][j];
}
else {
left += arr[i][j];
}
}
else if (i + j > N - 1 && i != j) {
if (i > j) {
bottom += arr[i][j];
}
else {
right += arr[i][j];
}
}
}
}
document.write(top + ' ' + left
+ ' ' + right + ' '
+ bottom + "<br>" );
}
let N = 4;
let arr = [ [ 1, 3, 1, 5 ],
[ 2, 2, 4, 1 ],
[ 5, 0, 2, 3 ],
[ 1, 3, 3, 5 ] ];
SumOfPartsOfMetrics(arr, N);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1) because It is using constant variable
Share your thoughts in the comments
Please Login to comment...