You are currently viewing Sum of Upper and Lower Triangles: Matrix Puzzle Solution

Sum of Upper and Lower Triangles: Matrix Puzzle Solution

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:

  1. Loop Through the Grid:
    • Start from the top-left corner of the grid.
    • Move through each row and column one by one.
  2. 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.
  3. Summing Up:
    • Keep track of the total sum for the upper triangle and the lower triangle separately.
  4. 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

  1. Initialization: It sets upper_sum and lower_sum initially to zero to keep track of the sums for the upper and lower triangles.
  2. 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) and for j in range(n) go through each element’s row i and column j.
  3. Calculating Upper Triangle Sum:
    • For each element in the matrix, it checks if i (the current row) is less than or equal to j (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.
  4. Calculating Lower Triangle Sum:
    • Similarly, it checks if i is greater than or equal to j.
    • If true, it means the element is in the lower triangle, including the diagonal. It adds the element’s value to the lower_sum.
  5. 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.

Leave a Reply