Matrix Aggregation One Shot

86.1k
0

Matrix Aggregation One Shot

What is Matrix Aggregation?

A matrix stores values in rows and columns. Aggregation means collecting values from the matrix and combining them into a smaller, meaningful result instead of printing every element.

Common aggregation results include:

  • Sum of all elements

  • Row-wise sum, Column-wise sum

  • Minimum element, Maximum element

  • Count of even or odd elements

  • Main diagonal sum, Anti-diagonal sum

  • Sum of both diagonals


How Matrix Aggregation Works

Most aggregation problems follow the same simple process:

  1. Initialize a variable such as sum, count, minimum, or maximum.

  2. Traverse the required matrix cells.

  3. Update the variable using the current cell.

  4. Return the final result after traversal completes.

The only difference between problems is which cells are visited.

Sum of Matrix

Given a matrix with N rows and M columns, find the sum of all elements present in the matrix.

For a detailed explanation, see the original TUF blog: Sum of Matrix

Algorithm

  • Initialize the sum as 0.

  • Traverse every row using an outer loop.

  • For each row, traverse all columns using an inner loop.

  • Add the current element to the running sum.

  • Return the final sum after all elements have been processed.

Complexity Analysis

Time Complexity: O(N × M), every matrix cell is visited exactly once.

Space Complexity: O(1), only a running sum and loop variables are used.

Row-Wise Sum in a Matrix

Given a matrix with N rows and M columns, find the sum of elements for every row.

For a detailed explanation, see the original TUF blog: Row Wise Sum

Algorithm

  • Initialize an empty result array.

  • Traverse every row of the matrix.

  • Initialize the current row sum as 0.

  • Traverse all columns and add each element to the row sum.

  • Store the row sum in the result array.

  • Return the result array after all rows have been processed.

Complexity Analysis

Time Complexity: O(N × M), every matrix cell is visited exactly once.

Space Complexity: O(N), result array stores one sum for every row.

Column-Wise Sum in a Matrix

Given a matrix with N rows and M columns, find the sum of elements for every column.

For a detailed explanation, see the original TUF blog: Column Wise Sum

Algorithm

  • Initialize an empty result array.

  • Traverse every column of the matrix.

  • Initialize the current column sum as 0.

  • Traverse all rows and add each element to the column sum.

  • Store the column sum in the result array.

  • Return the result array after all columns have been processed.

Complexity Analysis

Time Complexity: O(N × M), every matrix cell is visited exactly once.

Space Complexity: O(M), result array stores one sum for every column.

Maximum and Minimum Element in a Matrix

Given a matrix with N rows and M columns, find the maximum element and minimum element present in the matrix.

For a detailed explanation, see the original TUF blog: Min/Max Element

Algorithm

  • Initialize the minimum and maximum with the first matrix element.

  • Traverse every row of the matrix.

  • For each row, traverse all columns.

  • Update the minimum and maximum using the current element.

  • Return both the minimum and maximum after all elements have been processed.

Complexity Analysis

Time Complexity: O(N × M), every matrix cell is visited exactly once.

Space Complexity: O(1), only two running values and loop variables are used.

Even and Odd Count in a Matrix

Given a matrix with N rows and M columns, count the number of even elements and odd elements present in the matrix.

For a detailed explanation, see the original TUF blog: Even/Odd count

Algorithm

  • Initialize the even and odd counters as 0.

  • Traverse every row of the matrix.

  • For each row, traverse all columns.

  • Check whether the current element is even or odd.

  • Update the corresponding counter.

  • Return both counts after all elements have been processed.

Complexity Analysis

Time Complexity: O(N × M), every matrix cell is visited exactly once.

Space Complexity: O(1), only two counters and loop variables are used.

Sum of Both Diagonals in a Matrix

Given a square matrix of size N x N, find the sum of elements present on both diagonals.

The center cell of an odd-sized matrix belongs to both diagonals and must be counted only once.

For a detailed explanation, see the original TUF blog: Sum of Both diagonals

Algorithm

  • Initialize the sum as 0.

  • Traverse every row of the matrix.

  • Add the current main diagonal element to the sum.

  • Compute the corresponding anti-diagonal column.

  • Add the anti-diagonal element only if it is different from the main diagonal element.

  • Return the final sum.

Complexity Analysis

Time Complexity: O(N), each row contributes at most two diagonal cells.

Space Complexity: O(1), only a running sum and loop variables are used.

Interview follow-up Questions

Total matrix sum uses an outer loop for rows and an inner loop for columns.

ArraysC++

Read Similar Blogs

Comments0