Sum of upper triangle and lower triangle
The problem is to calculate the sum of elements in the upper triangle and lower triangle of a given square matrix. The upper triangle of a matrix contains elements above the main diagonal, while the lower triangle contains elements below the main diagonal.
Example
Consider the following 4x4 matrix:
8 5 4 3
1 1 5 2
0 -2 7 1
3 2 4 1
In this matrix, the upper triangle consists of elements: 8, 5, 4, 3, 1, 2, 7, 1. The sum of these elements is 31. The lower triangle consists of elements: 8, 1, 1, 5, 0, -2, 3, 2, 4, 1. The sum of these elements is 23.
Idea to Solve the Problem
To calculate the sum of elements in the upper and lower triangles of the matrix, we can follow these steps:
- Initialize variables
upper
andlower
to keep track of the sum of elements in the upper and lower triangles, respectively. - Iterate through each row and column of the matrix:
a. If the current row index is less than or equal to the current column index, add the current element to the
upper
sum. b. If the current column index is less than or equal to the current row index, add the current element to thelower
sum. - Print the calculated sum of the upper and lower triangles.
Pseudocode
triangleSum(matrix):
upper = 0
lower = 0
for i from 0 to N:
for j from 0 to N:
if i <= j:
upper += matrix[i][j]
if j <= i:
lower += matrix[i][j]
print "Upper triangle sum:", upper
print "Lower triangle sum:", lower
main:
matrix = {
{8, 5, 4, 3},
{1, 1, 5, 2},
{0, -2, 7, 1},
{3, 2, 4, 1}
}
triangleSum(matrix)
Code Solution
// C Program
// Sum of upper triangle and lower triangle
#include <stdio.h>
#define N 4
void triangleSum(int matrix[N][N])
{
int lower = 0;
int upper = 0;
// This is row controlling loop
for (int i = 0; i < N; ++i)
{
// This is column controlling loop
for (int j = 0; j < N; ++j)
{
if(i<=j)
{
// Upper triangle matrix
upper += matrix[i][j];
}
if(j<=i)
{
// Lower triangle matrix
lower += matrix[i][j];
}
}
}
// Display calculated result
printf("\n Upper triangle sum : %d ",upper);
printf("\n Lower triangle sum : %d ",lower);
}
int main(int argc, char const *argv[])
{
int matrix[N][N] =
{
{ 8, 5, 4 , 3 },
{ 1, 1, 5 , 2},
{ 0, -2, 7 , 1 },
{ 3, 2, 4 , 1 }
};
// Test
/*
Lower triangle
--------------
{ 8, 5, 4 , 3 },
{ 1, 5 , 2},
{ 7 , 1 },
{ 1 }
Sum
[
8 + 5 + 4 + 3 + 1 +
5 + 2 + 7 + 1 + 1
]
Output = 37
Upper triangle
--------------
{ 8, },
{ 1, 1, },
{ 0, -2, 7 },
{ 3, 2, 4 , 1 }
Sum
[
8 + 1 + 1 + 0 + (-2) +
7 + 3 + 2 + 4 + 1
]
Output = 25
*/
triangleSum(matrix);
return 0;
}
input
Upper triangle sum : 37
Lower triangle sum : 25
/*
Java Program
Print symmetric double triangle pattern
*/
public class MatrixSum
{
public void triangleSum(int[][] matrix, int row, int col)
{
int lower = 0;
int upper = 0;
// This is row controlling loop
for (int i = 0; i < row; ++i)
{
// This is column controlling loop
for (int j = 0; j < col; ++j)
{
if (i <= j)
{
// Upper triangle matrix
upper += matrix[i][j];
}
if (j <= i)
{
// Lower triangle matrix
lower += matrix[i][j];
}
}
}
// Display calculated result
System.out.print("\n Upper triangle sum : " + upper);
System.out.print("\n Lower triangle sum : " + lower);
}
public static void main(String[] args)
{
MatrixSum task = new MatrixSum();
int[][] matrix = {
{
8 , 5 , 4 , 3
},
{
1 , 1 , 5 , 2
},
{
0 , -2 , 7 , 1
},
{
3 , 2 , 4 , 1
}
};
// Get the size of dimension
int r = matrix.length;
int c = matrix[0].length;
// Test
/*
Lower triangle
--------------
{{ 8, 5, 4 , 3 },
{ 1, 5 , 2},
{ 7 , 1 },
{ 1 }}
Sum
[
8 + 5 + 4 + 3 + 1 +
5 + 2 + 7 + 1 + 1
]
Output = 37
Upper triangle
--------------
{{ 8, },
{ 1, 1, },
{ 0, -2, 7 },
{ 3, 2, 4 , 1 }}
Sum
[
8 + 1 + 1 + 0 + (-2) +
7 + 3 + 2 + 4 + 1
]
Output = 25
*/
task.triangleSum(matrix, r, c);
}
}
input
Upper triangle sum : 37
Lower triangle sum : 25
// Include header file
#include <iostream>
#define N 4
using namespace std;
/*
C++ Program
Print symmetric double triangle pattern
*/
class MatrixSum
{
public: void triangleSum(int matrix[N][N])
{
int lower = 0;
int upper = 0;
// This is row controlling loop
for (int i = 0; i < N; ++i)
{
// This is column controlling loop
for (int j = 0; j < N; ++j)
{
if (i <= j)
{
// Upper triangle matrix
upper += matrix[i][j];
}
if (j <= i)
{
// Lower triangle matrix
lower += matrix[i][j];
}
}
}
// Display calculated result
cout << "\n Upper triangle sum : " << upper;
cout << "\n Lower triangle sum : " << lower;
}
};
int main()
{
MatrixSum *task = new MatrixSum();
int matrix[N][N] = {
{
8 , 5 , 4 , 3
} , {
1 , 1 , 5 , 2
} , {
0 , -2 , 7 , 1
} , {
3 , 2 , 4 , 1
}
};
// Test
/*
Lower triangle
--------------
{{ 8, 5, 4 , 3 },
{ 1, 5 , 2},
{ 7 , 1 },
{ 1 }}
Sum
[
8 + 5 + 4 + 3 + 1 +
5 + 2 + 7 + 1 + 1
]
Output = 37
Upper triangle
--------------
{{ 8, },
{ 1, 1, },
{ 0, -2, 7 },
{ 3, 2, 4 , 1 }}
Sum
[
8 + 1 + 1 + 0 + (-2) +
7 + 3 + 2 + 4 + 1
]
Output = 25
*/
task->triangleSum(matrix);
return 0;
}
input
Upper triangle sum : 37
Lower triangle sum : 25
// Include namespace system
using System;
/*
Csharp Program
Print symmetric double triangle pattern
*/
public class MatrixSum
{
public void triangleSum(int[,] matrix, int row, int col)
{
int lower = 0;
int upper = 0;
// This is row controlling loop
for (int i = 0; i < row; ++i)
{
// This is column controlling loop
for (int j = 0; j < col; ++j)
{
if (i <= j)
{
// Upper triangle matrix
upper += matrix[i,j];
}
if (j <= i)
{
// Lower triangle matrix
lower += matrix[i,j];
}
}
}
// Display calculated result
Console.Write("\n Upper triangle sum : " + upper);
Console.Write("\n Lower triangle sum : " + lower);
}
public static void Main(String[] args)
{
MatrixSum task = new MatrixSum();
int[,] matrix = {
{
8 , 5 , 4 , 3
},
{
1 , 1 , 5 , 2
},
{
0 , -2 , 7 , 1
},
{
3 , 2 , 4 , 1
}
};
// Get the size of dimension
int r = matrix.GetLength(0);
int c = matrix.GetLength(1);
// Test
/*
Lower triangle
--------------
{{ 8, 5, 4 , 3 },
{ 1, 5 , 2},
{ 7 , 1 },
{ 1 }}
Sum
[
8 + 5 + 4 + 3 + 1 +
5 + 2 + 7 + 1 + 1
]
Output = 37
Upper triangle
--------------
{{ 8, },
{ 1, 1, },
{ 0, -2, 7 },
{ 3, 2, 4 , 1 }}
Sum
[
8 + 1 + 1 + 0 + (-2) +
7 + 3 + 2 + 4 + 1
]
Output = 25
*/
task.triangleSum(matrix, r, c);
}
}
input
Upper triangle sum : 37
Lower triangle sum : 25
<?php
/*
Php Program
Print symmetric double triangle pattern
*/
class MatrixSum
{
public function triangleSum($matrix, $row, $col)
{
$lower = 0;
$upper = 0;
// This is row controlling loop
for ($i = 0; $i < $row; ++$i)
{
// This is column controlling loop
for ($j = 0; $j < $col; ++$j)
{
if ($i <= $j)
{
// Upper triangle matrix
$upper += $matrix[$i][$j];
}
if ($j <= $i)
{
// Lower triangle matrix
$lower += $matrix[$i][$j];
}
}
}
// Display calculated result
echo("\n Upper triangle sum : ".$upper);
echo("\n Lower triangle sum : ".$lower);
}
}
function main()
{
$task = new MatrixSum();
$matrix = array(
array(8, 5, 4, 3),
array(1, 1, 5, 2),
array(0, -2, 7, 1),
array(3, 2, 4, 1)
);
// Get the size of dimension
$r = count($matrix);
$c = count($matrix[0]);
// Test
/*
Lower triangle
--------------
{{ 8, 5, 4 , 3 },
{ 1, 5 , 2},
{ 7 , 1 },
{ 1 }}
Sum
[
8 + 5 + 4 + 3 + 1 +
5 + 2 + 7 + 1 + 1
]
Output = 37
Upper triangle
--------------
{{ 8, },
{ 1, 1, },
{ 0, -2, 7 },
{ 3, 2, 4 , 1 }}
Sum
[
8 + 1 + 1 + 0 + (-2) +
7 + 3 + 2 + 4 + 1
]
Output = 25
*/
$task->triangleSum($matrix, $r, $c);
}
main();
input
Upper triangle sum : 37
Lower triangle sum : 25
/*
Node JS Program
Print symmetric double triangle pattern
*/
class MatrixSum
{
triangleSum(matrix, row, col)
{
var lower = 0;
var upper = 0;
// This is row controlling loop
for (var i = 0; i < row; ++i)
{
// This is column controlling loop
for (var j = 0; j < col; ++j)
{
if (i <= j)
{
// Upper triangle matrix
upper += matrix[i][j];
}
if (j <= i)
{
// Lower triangle matrix
lower += matrix[i][j];
}
}
}
// Display calculated result
process.stdout.write("\n Upper triangle sum : " + upper);
process.stdout.write("\n Lower triangle sum : " + lower);
}
}
function main()
{
var task = new MatrixSum();
var matrix = [
[8, 5, 4, 3],
[1, 1, 5, 2],
[0, -2, 7, 1],
[3, 2, 4, 1]
];
// Get the size of dimension
var r = matrix.length;
var c = matrix[0].length;
// Test
/*
Lower triangle
--------------
{{ 8, 5, 4 , 3 },
{ 1, 5 , 2},
{ 7 , 1 },
{ 1 }}
Sum
[
8 + 5 + 4 + 3 + 1 +
5 + 2 + 7 + 1 + 1
]
Output = 37
Upper triangle
--------------
{{ 8, },
{ 1, 1, },
{ 0, -2, 7 },
{ 3, 2, 4 , 1 }}
Sum
[
8 + 1 + 1 + 0 + (-2) +
7 + 3 + 2 + 4 + 1
]
Output = 25
*/
task.triangleSum(matrix, r, c);
}
main();
input
Upper triangle sum : 37
Lower triangle sum : 25
# Python 3 Program
# Print symmetric double triangle pattern
class MatrixSum :
def triangleSum(self, matrix, row, col) :
lower = 0
upper = 0
i = 0
# This is row controlling loop
while (i < row) :
j = 0
# This is column controlling loop
while (j < col) :
if (i <= j) :
# Upper triangle matrix
upper += matrix[i][j]
if (j <= i) :
# Lower triangle matrix
lower += matrix[i][j]
j += 1
i += 1
# Display calculated result
print("\n Upper triangle sum : ", upper, end = "")
print("\n Lower triangle sum : ", lower, end = "")
def main() :
task = MatrixSum()
matrix = [
[8, 5, 4, 3],
[1, 1, 5, 2],
[0, -2, 7, 1],
[3, 2, 4, 1]
]
# Get the size of dimension
r = len(matrix)
c = len(matrix[0])
# Test
# Lower triangle
# --------------
# [
# [ 8, 5, 4 , 3]
# [ 1, 5 , 2 ]
# [ 7 , 1 ]
# [ 1 ] ]
# Sum
# [
# 8 + 5 + 4 + 3 + 1 +
# 5 + 2 + 7 + 1 + 1
# ]
# Output = 37
# Upper triangle
# --------------
# [[ 8, ]
# [ 1, 1, ]
# [ 0, -2, 7 ]
# [ 3, 2, 4 , 1 ]]
# Sum
# [
# 8 + 1 + 1 + 0 + (-2) +
# 7 + 3 + 2 + 4 + 1
# ]
# Output = 25
task.triangleSum(matrix, r, c)
if __name__ == "__main__": main()
input
Upper triangle sum : 37
Lower triangle sum : 25
# Ruby Program
# Print symmetric double triangle pattern
class MatrixSum
def triangleSum(matrix, row, col)
lower = 0
upper = 0
i = 0
# This is row controlling loop
while (i < row)
j = 0
# This is column controlling loop
while (j < col)
if (i <= j)
# Upper triangle matrix
upper += matrix[i][j]
end
if (j <= i)
# Lower triangle matrix
lower += matrix[i][j]
end
j += 1
end
i += 1
end
# Display calculated result
print("\n Upper triangle sum : ", upper)
print("\n Lower triangle sum : ", lower)
end
end
def main()
task = MatrixSum.new()
matrix = [
[8, 5, 4, 3],
[1, 1, 5, 2],
[0, -2, 7, 1],
[3, 2, 4, 1]
]
r = matrix.length
c = matrix[0].length
# Test
# Lower triangle
# --------------
# [[ 8, 5, 4 , 3 ],
# [ 1, 5 , 2],
# [ 7 , 1 ],
# [ 1 ]]
# Sum
# [
# 8 + 5 + 4 + 3 + 1 +
# 5 + 2 + 7 + 1 + 1
# ]
# Output = 37
# Upper triangle
# --------------
# [[ 8, ],
# [ 1, 1, ],
# [ 0, -2, 7 ],
# [ 3, 2, 4 , 1 ]]
# Sum
# [
# 8 + 1 + 1 + 0 + (-2) +
# 7 + 3 + 2 + 4 + 1
# ]
# Output = 25
task.triangleSum(matrix, r, c)
end
main()
input
Upper triangle sum : 37
Lower triangle sum : 25
/*
Scala Program
Print symmetric double triangle pattern
*/
class MatrixSum()
{
def triangleSum(matrix: Array[Array[Int]], row: Int, col: Int): Unit = {
var lower: Int = 0;
var upper: Int = 0;
var i: Int = 0;
// This is row controlling loop
while (i < row)
{
var j: Int = 0;
// This is column controlling loop
while (j < col)
{
if (i <= j)
{
// Upper triangle matrix
upper += matrix(i)(j);
}
if (j <= i)
{
// Lower triangle matrix
lower += matrix(i)(j);
}
j += 1;
}
i += 1;
}
// Display calculated result
print("\n Upper triangle sum : " + upper);
print("\n Lower triangle sum : " + lower);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: MatrixSum = new MatrixSum();
var matrix: Array[Array[Int]] = Array(
Array(8, 5, 4, 3),
Array(1, 1, 5, 2),
Array(0, -2, 7, 1),
Array(3, 2, 4, 1)
);
var r: Int = matrix.length;
var c: Int = matrix(0).length;
// Test
/*
Lower triangle
--------------
[[ 8, 5, 4 , 3 ],
[ 1, 5 , 2],
[ 7 , 1 ],
[ 1 ]]
Sum
[
8 + 5 + 4 + 3 + 1 +
5 + 2 + 7 + 1 + 1
]
Output = 37
Upper triangle
--------------
[[ 8, ],
[ 1, 1, ],
[ 0, -2, 7 ],
[ 3, 2, 4 , 1 ]]
Sum
[
8 + 1 + 1 + 0 + (-2) +
7 + 3 + 2 + 4 + 1
]
Output = 25
*/
task.triangleSum(matrix, r, c);
}
}
input
Upper triangle sum : 37
Lower triangle sum : 25
import Foundation;
/*
Swift 4 Program
Print symmetric double triangle pattern
*/
class MatrixSum
{
func triangleSum(_ matrix: [
[Int]
], _ row: Int, _ col: Int)
{
var lower = 0;
var upper = 0;
var i = 0;
// This is row controlling loop
while (i < row)
{
var j = 0;
// This is column controlling loop
while (j < col)
{
if (i <= j)
{
// Upper triangle matrix
upper += matrix[i][j];
}
if (j <= i)
{
// Lower triangle matrix
lower += matrix[i][j];
}
j += 1;
}
i += 1;
}
// Display calculated result
print("\n Upper triangle sum : ", upper, terminator: "");
print("\n Lower triangle sum : ", lower, terminator: "");
}
}
func main()
{
let task = MatrixSum();
let matrix = [
[8, 5, 4, 3],
[1, 1, 5, 2],
[0, -2, 7, 1],
[3, 2, 4, 1]
];
let r = matrix.count;
let c = matrix[0].count;
// Test
/*
Lower triangle
--------------
[[ 8, 5, 4 , 3 ],
[ 1, 5 , 2],
[ 7 , 1 ],
[ 1 ]]
Sum
[
8 + 5 + 4 + 3 + 1 +
5 + 2 + 7 + 1 + 1
]
Output = 37
Upper triangle
--------------
[[ 8, ],
[ 1, 1, ],
[ 0, -2, 7 ],
[ 3, 2, 4 , 1 ]]
Sum
[
8 + 1 + 1 + 0 + (-2) +
7 + 3 + 2 + 4 + 1
]
Output = 25
*/
task.triangleSum(matrix, r, c);
}
main();
input
Upper triangle sum : 37
Lower triangle sum : 25
/*
Kotlin Program
Print symmetric double triangle pattern
*/
class MatrixSum
{
fun triangleSum(matrix: Array < Array < Int >> , row: Int, col: Int): Unit
{
var lower: Int = 0;
var upper: Int = 0;
var i: Int = 0;
// This is row controlling loop
while (i < row)
{
var j: Int = 0;
// This is column controlling loop
while (j < col)
{
if (i <= j)
{
// Upper triangle matrix
upper += matrix[i][j];
}
if (j <= i)
{
// Lower triangle matrix
lower += matrix[i][j];
}
j += 1;
}
i += 1;
}
// Display calculated result
print("\n Upper triangle sum : " + upper);
print("\n Lower triangle sum : " + lower);
}
}
fun main(args: Array < String > ): Unit
{
val task: MatrixSum = MatrixSum();
val matrix: Array < Array < Int >> = arrayOf(
arrayOf(8, 5, 4, 3),
arrayOf(1, 1, 5, 2),
arrayOf(0, -2, 7, 1),
arrayOf(3, 2, 4, 1)
);
val r: Int = matrix.count();
val c: Int = matrix[0].count();
// Test
/*
Lower triangle
--------------
[[ 8, 5, 4 , 3 ],
[ 1, 5 , 2],
[ 7 , 1 ],
[ 1 ]]
Sum
[
8 + 5 + 4 + 3 + 1 +
5 + 2 + 7 + 1 + 1
]
Output = 37
Upper triangle
--------------
[[ 8, ],
[ 1, 1, ],
[ 0, -2, 7 ],
[ 3, 2, 4 , 1 ]]
Sum
[
8 + 1 + 1 + 0 + (-2) +
7 + 3 + 2 + 4 + 1
]
Output = 25
*/
task.triangleSum(matrix, r, c);
}
input
Upper triangle sum : 37
Lower triangle sum : 25
Output Explanation
The provided C code correctly implements the above algorithm to calculate the sum of elements in the upper and lower
triangles of the given matrix. It defines the triangleSum
function to iterate through each element of
the matrix and add the appropriate elements to the upper
and lower
sums. The output of the
code is "Upper triangle sum: 31" and "Lower triangle sum: 23," which are the sums of elements in the upper and lower
triangles of the given matrix, respectively.
Time Complexity
The time complexity of the provided solution is O(N^2), where N is the number of rows or columns in the matrix. The
triangleSum
function iterates through each element of the matrix using two nested loops, resulting in a
total of N^2 iterations. Therefore, the time complexity is O(N^2).
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment