Sum of non-diagonal parts of a square Matrix
Last Updated :
12 Nov, 2023
\Given a square matrix of size N X N, the task is to find the sum of all elements at each portion when the matrix is divided into four parts along its diagonals. The elements at the diagonals should not be counted in the sum.
Examples:
Input: arr[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}
Output: 68
Explanation:
From the above image, (1, 6, 11, 16) and (4, 7, 10, 13) are the diagonals.
The sum of the elements needs to be found are:
Top: (2 + 3) = 5
Left: (5 + 9) = 14
Bottom: (14 + 15) = 29
Right: (8 + 12) = 20
Therefore, sum of all parts = 68.
Input: arr[][] = { {1, 3, 1, 5}, {2, 2, 4, 1}, {5, 0, 2, 3}, {1, 3, 1, 5}}
Output: 19
Approach: The idea is to use indexing to identify the elements at the diagonals.
- In a 2-dimensional matrix, two diagonals are identified in the following way:
- Principal Diagonal: The first diagonal has the index of the row is equal to the index of the column.
Condition for Principal Diagonal:
The row-column condition is row = column.
-
- Secondary Diagonal: The second diagonal has the sum of the index of row and column equal to N(size of the matrix).
Condition for Secondary Diagonal:
The row-column condition is row = numberOfRows - column -1
-
- After identifying both the diagonals, the matrix can further be divided into two parts using the diagonal passing through the first element of the last row and the last element of the first row:
- The left part:
- If the column index is greater than row index, the element belongs to the top portion of the matrix.
- If the row index is greater than column index, the element belongs to the left portion of the matrix.
- The right part:
- If the column index is greater than row index, the element belongs to the right portion of the matrix.
- If the row index is greater than column index, the element belongs to the bottom portion of the matrix.
- So in order to get the sum of the non-diagonal parts of the matrix:
- Traverse the matrix rowwise
- If the element is a part of diagonal, then skip this element
- If the element is part of the left, right, bottom, or top part (i.e. non-diagonal parts), add the element in the resultant sum
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int sumOfParts( int * arr, int N)
{
int sum_part1 = 0, sum_part2 = 0,
sum_part3 = 0, sum_part4 = 0;
int totalsum = 0;
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < N; j++) {
if (i + j < N - 1) {
if (i < j and i != j and i + j)
sum_part1 += (arr + i * N)[j];
else if (i != j)
sum_part2 += (arr + i * N)[j];
}
else {
if (i > j and i + j != N - 1)
sum_part3 += (arr + i * N)[j];
else {
if (i + j != N - 1 and i != j)
sum_part4 += (arr + i * N)[j];
}
}
}
}
totalsum = sum_part1 + sum_part2
+ sum_part3 + sum_part4;
return totalsum;
}
int main()
{
int N = 4;
int arr[N][N] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
cout << sumOfParts(( int *)arr, N);
}
|
Java
class GFG
{
static int sumOfParts( int [][] arr, int N)
{
int sum_part1 = 0 , sum_part2 = 0 ,
sum_part3 = 0 , sum_part4 = 0 ;
int totalsum = 0 ;
for ( int i = 0 ; i < N; i++) {
for ( int j = 0 ; j < N; j++) {
if (i + j < N - 1 ) {
if (i < j && i != j && i + j > 0 )
sum_part1 += arr[i][j];
else if (i != j)
sum_part2 += arr[i][j];
}
else {
if (i > j && i + j != N - 1 )
sum_part3 += arr[i][j];
else {
if (i + j != N - 1 && i != j)
sum_part4 += arr[i][j];
}
}
}
}
totalsum = sum_part1 + sum_part2
+ sum_part3 + sum_part4;
return totalsum;
}
public static void main(String[] args)
{
int N = 4 ;
int arr[][] = { { 1 , 2 , 3 , 4 },
{ 5 , 6 , 7 , 8 },
{ 9 , 10 , 11 , 12 },
{ 13 , 14 , 15 , 16 } };
System.out.print(sumOfParts(arr, N));
}
}
|
Python3
def sumOfParts(arr,N):
sum_part1, sum_part2, sum_part3, \
sum_part4 = 0 , 0 , 0 , 0
totalsum = 0
for i in range (N):
for j in range (N):
if i + j < N - 1 :
if (i < j and i ! = j and i + j):
sum_part1 + = arr[i][j]
elif i ! = j:
sum_part2 + = arr[i][j]
else :
if i > j and i + j ! = N - 1 :
sum_part3 + = arr[i][j]
else :
if i + j ! = N - 1 and i ! = j:
sum_part4 + = arr[i][j]
return sum_part1 + sum_part2 + sum_part3 + sum_part4
N = 4
arr = [[ 1 , 2 , 3 , 4 ],
[ 5 , 6 , 7 , 8 ],
[ 9 , 10 , 11 , 12 ],
[ 13 , 14 , 15 , 16 ]]
print (sumOfParts(arr, N))
|
C#
using System;
class GFG
{
static int sumOfParts( int [,] arr, int N)
{
int sum_part1 = 0, sum_part2 = 0,
sum_part3 = 0, sum_part4 = 0;
int totalsum = 0;
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < N; j++) {
if (i + j < N - 1) {
if (i < j && i != j && i + j > 0)
sum_part1 += arr[i, j];
else if (i != j)
sum_part2 += arr[i, j];
}
else {
if (i > j && i + j != N - 1)
sum_part3 += arr[i, j];
else {
if (i + j != N - 1 && i != j)
sum_part4 += arr[i, j];
}
}
}
}
totalsum = sum_part1 + sum_part2
+ sum_part3 + sum_part4;
return totalsum;
}
public static void Main()
{
int N = 4;
int [,]arr = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
Console.WriteLine(sumOfParts(arr, N));
}
}
|
Javascript
<script>
function sumOfParts(arr , N)
{
var sum_part1 = 0, sum_part2 = 0,
sum_part3 = 0, sum_part4 = 0;
var totalsum = 0;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
if (i + j < N - 1) {
if (i < j && i != j && i + j > 0)
sum_part1 += arr[i][j];
else if (i != j)
sum_part2 += arr[i][j];
}
else {
if (i > j && i + j != N - 1)
sum_part3 += arr[i][j];
else {
if (i + j != N - 1 && i != j)
sum_part4 += arr[i][j];
}
}
}
}
totalsum = sum_part1 + sum_part2
+ sum_part3 + sum_part4;
return totalsum;
}
var N = 4;
var arr = [ [ 1, 2, 3, 4 ],
[ 5, 6, 7, 8 ],
[ 9, 10, 11, 12 ],
[ 13, 14, 15, 16 ] ];
document.write(sumOfParts(arr, N));
</script>
|
Time Complexity: O(N2) as we are traversing the complete matrix row-wise.
Auxiliary Space: O(1)
Using Two Nested Loops:
Approach:
Define a function sum_non_diagonal that takes in a 2D matrix as input.
Get the length of the matrix (assuming it’s square) and initialize a variable sum to 0.
Use two nested loops to iterate over the rows and columns of the matrix.
Check if the current element is not on the diagonal (i.e., if i is not equal to j and i is not equal to n – j – 1, where n is the length of the matrix).
If the current element is not on the diagonal, add it to the sum.
Return the sum.
C++
#include <iostream>
using namespace std;
int sumNonDiagonal( int matrix[4][4]) {
int n = 4;
int sum = 0;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
if (i != j && i != n - j - 1) {
sum += matrix[i][j];
}
}
}
return sum;
}
int main() {
int matrix[4][4] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}};
cout << sumNonDiagonal(matrix) << endl;
return 0;
}
|
Java
import java.io.*;
public class GFG {
public static int sumNonDiagonal( int [][] matrix)
{
int n = matrix.length;
int sum = 0 ;
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j < n; j++)
{
if (i != j && i != n - j - 1 )
{
sum += matrix[i][j];
}
}
}
return sum;
}
public static void main(String[] args)
{
int [][] matrix = { { 1 , 2 , 3 , 4 },
{ 5 , 6 , 7 , 8 },
{ 9 , 10 , 11 , 12 },
{ 13 , 14 , 15 , 16 } };
System.out.println(
sumNonDiagonal(matrix));
}
}
|
Python3
def sum_non_diagonal(matrix):
n = len (matrix)
sum = 0
for i in range (n):
for j in range (n):
if i ! = j and i ! = n - j - 1 :
sum + = matrix[i][j]
return sum
matrix = [[ 1 , 2 , 3 , 4 ], [ 5 , 6 , 7 , 8 ], [ 9 , 10 , 11 , 12 ], [ 13 , 14 , 15 , 16 ]]
print (sum_non_diagonal(matrix))
|
C#
using System;
class Program {
static int SumNonDiagonal( int [, ] matrix)
{
int n = 4;
int sum = 0;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
if (i != j && i != n - j - 1) {
sum += matrix[i, j];
}
}
}
return sum;
}
static void Main( string [] args)
{
int [, ] matrix = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
Console.WriteLine(
SumNonDiagonal(matrix));
}
}
|
Javascript
function sum_non_diagonal(matrix) {
let n = matrix.length;
let sum = 0;
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
if (i != j && i != n - j - 1) {
sum += matrix[i][j];
}
}
}
return sum;
}
let matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]];
console.log(sum_non_diagonal(matrix));
|
Time Complexity: O(n^2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...