Leetcode - Matrix Diagonal Sum / Difference


Given a quare matrix mat, return the sum of the matrix diagonals.

Only include the sum of all the elements on the primary diagonal and all the elements on the secondary diagonal that are not part of the primary diagonal.

Examples: 

Input: mat = [[1,2,3], [4,5,6], [7,8,9]] 
Output: 25 Explanation: 
Diagonals sum: 1 + 5 + 9 + 3 + 7 = 25 
Notice that element mat[1][1] = 5 is counted only once because 5 is part of the primary diagonal in secondary diagonal. 

Input: mat = [[1,1,1,1], [1,1,1,1], [1,1,1,1], [1,1,1,1]] 
Output: 8

Implementation
 int diagonalSumSingleTraverse(vector<vector <int>> &matrix)
{
    int size = matrix.size();
    int sum = 0;
    for (int i = 0; i < size; i++)
    {
        sum += matrix[i][i] + matrix[i][size - i - 1]; // (size-i-1) = 3-0-1 = 2, 3-1-1 = 1, 3-2-1 = 0
    }
    if (size % 2 != 0)
    {
        return sum - matrix[size / 2][size / 2];
    }
    return sum;
}
This problem isn't that complicated at all. Middle element that can be common and be a part of a secondary diagonal only if there's even number of elements. 

Thus, we are checking that if size%2 != 0 then we know there's a center element that can be a part of the primary diagonal in secondary one so we can simply just subtract it. 

The full source code for this problem is here -- MatrixDiagonalSum.cpp

Diagonal Difference


Now there's a similar type of question that I came across in Hackerrank website. It is sort of a similar question but rather than addition, we are performing subtraction.

So the problem statement goes as follows :

    Given a square matrix of size N×N l N×N, calculate the absolute difference between the sums of its diagonals.

    Here in this challenge, there's no constraints and simply asked to provide the absolute value of difference, so  we don't require to perform extra step to find the middle value as we did in the matrix diagonal sum challenge.

Sample Input
3
11 2 4
4 5 6
10 8 -12

Sample Output15

Explanation

The primary diagonal is:
11
      5
            -12

Sum across the primary diagonal: 11 + 5 - 12 = 4

The secondary diagonal is:
            4
      5
10
Sum across the secondary diagonal: 4 + 5 + 10 = 19

Difference: |4 - 19| = 15

Implementation 
int diagonalDiff(vector<vector<int>> matrixV)
{
    int sum1 = 0;
    int sum2 = 0;
    int size = matrixV.size(); 
    for (int i = 0; i < size; i++)
    {
        sum1 += matrixV[i][i];
        sum2 += matrixV[i][size-i-1];
    }
    return abs(sum1 - sum2);
}
Diagonal different is simple and straightforward problem. Just need to figure out how to get the second diagonal indexes. 

Instead of having separate value of j we can simply write size-i-1, to obtain the secondary diagonal.
sum2 += matrixV[i][size-i-1];
The above statement is eliminates the need for writing complicated for loop that involves having a separate indicator j for tracking secondary diagonal. 
 for ( int i= 0 , j = matrixV.size()-1 ; i < matrixV.size(); i++, j--){
   
   sum1 = sum1 + twoDimArray[i][i];
   sum2 = sum2 + twoDimArray[i][j];
   
  }
Both solutions are valid and work, but you can see how first solution simplifies the loop by not having a separate indicator j. I have tried to complete this challenge both with vector and array. I am not posting here, but you can checkout my full source code to see the detailed implementation. 

Always with every code challenge, try your best to solve yourself first. If you cannot, then and then only try to look for the solution. I always look for solution only after I solve it myself to see if there's any improvements that can be made.

Check the full source code for diagonal difference here -- diagonalDifference.cpp

Post a Comment

0 Comments