Introduction:
Exploring the computation of the ‘Sum of upper and lower triangles’ within matrices is fundamental in programming. This blog post delves into a Python-based solution that demonstrates an efficient approach to solving this common coding challenge.
Problem Statement:
Given a square matrix of size N×N, we are tasked with finding and summing the elements in the upper and lower triangles of the matrix. The upper triangle includes elements on the diagonal and above it, while the lower triangle includes elements on the diagonal and below it.
Example:
Let’s take an example matrix:
N = 3
mat[][] = [
[6, 5, 4],
[1, 2, 5],
[7, 9, 7]
]
The elements of the upper triangle are:
6 5 4
2 5
7
Sum of these elements: 6+5+4+2+5+7=296+5+4+2+5+7=29.
The elements of the lower triangle are:
6
1 2
7 9 7
Sum of these elements: 6+1+2+7+9+7=326+1+2+7+9+7=32.
Hence, the expected output for this matrix is [29,32][29,32].
Approach:
- Loop Through the Grid:
- Start from the top-left corner of the grid.
- Move through each row and column one by one.
- Identify Upper and Lower Triangles:
- For the upper triangle:
- Add the numbers on or above the diagonal.
- For the lower triangle:
- Add the numbers on or below the diagonal.
- For the upper triangle:
- Summing Up:
- Keep track of the total sum for the upper triangle and the lower triangle separately.
- Return the Sums:
- Once you’ve looped through the entire grid and added the numbers in the triangles, return the total sums for both the upper and lower triangles.
Example (Simple Illustration):
Consider the grid:
6 5 4
1 2 5
7 9 7
Starting from the top-left:
- For the upper triangle: Add 6, 5, 4, 2, 5, and 7 (above the diagonal).
- For the lower triangle: Add 6, 1, 2, 7, 9, and 7 (below the diagonal).
Return the sums for both the upper and lower triangles.
Solution: (Python)
class Solution:
# Function to return sum of upper and lower triangles of a matrix.
def sumTriangles(self, matrix, n):
upper_sum = 0
lower_sum = 0
for i in range(n):
for j in range(n):
if i <= j: # Upper triangle (including diagonal)
upper_sum += matrix[i][j]
if i >= j: # Lower triangle (including diagonal)
lower_sum += matrix[i][j]
return [upper_sum, lower_sum]
Explanation About Code
- Initialization: It sets
upper_sum
andlower_sum
initially to zero to keep track of the sums for the upper and lower triangles. - Looping through the Matrix:
- Using two nested
for
loops, it iterates through each row and column of the square matrix. for i in range(n)
andfor j in range(n)
go through each element’s rowi
and columnj
.
- Using two nested
- Calculating Upper Triangle Sum:
- For each element in the matrix, it checks if
i
(the current row) is less than or equal toj
(the current column). - If it’s true, it means the element is in the upper triangle, including the diagonal. It adds the element’s value to the
upper_sum
.
- For each element in the matrix, it checks if
- Calculating Lower Triangle Sum:
- Similarly, it checks if
i
is greater than or equal toj
. - If true, it means the element is in the lower triangle, including the diagonal. It adds the element’s value to the
lower_sum
.
- Similarly, it checks if
- Returning the Sums:
- After iterating through the entire matrix, it collects the total sums of the upper and lower triangles.
- Finally, it returns these sums as a list containing
[upper_sum, lower_sum]
.
The algorithm distinguishes the upper and lower triangle areas by comparing row and column indices. If i <= j
, it accumulates the sum in the upper_sum
as it represents the upper triangle. Similarly, if i >= j
, it adds to the lower_sum
for the lower triangle.
Conclusion
Comprehending the ‘Sum of upper and lower triangles‘ within matrices is a crucial skill in programming. Exploring our comprehensive Python solution equips you to adeptly handle matrix computations for both upper and lower triangles, advancing your problem-solving abilities.