JavaScript Program to check if the matrix is lower Triangular


A matrix can be defined as a 2D array that stores elements in it and mathematically it stores the numbers in it. A Lower triangular matrix is a squared matrix that has the same number of rows and columns and all the elements that are present above the main diagonal passing from the first cell (present at the top-left) towards the last cell (present at the bottom-right) are zero. We will implement a proper code with explanation and discussion over time and space complexity.

Example

Input 1:
mat = [ [ 1, 0, 0, 0],
   [ 2, 3, 0, 0],
   [4, 5, 6, 0],
   [7, 8, 9, 1]
 ]
Output 1: Yes,

Explanation: We can see that the main diagonal contains the elements 1, 3, 6, and 1 and all the cells present above the main diagonal has the value zero.

Input 2:
mat = [ [ 1, 0, 0, 1],
   [ 2, 3, 0, 0],
   [4, 5, 6, 0],
   [7, 8, 9, 1]
 ]
Output 1: No

Explanation: We can see that the main diagonal contains the elements 1, 3, 6, and 1 and all the cells present above the main diagonal don’t have the value zero.

Approach

We have seen an example above, now let us see the steps to implement the code:

First, we will create a function in which we will pass the given matrix. We will traverse over the matrix only in the part which is upper side of the main diagonal that is for each cell (i,j) where j is greater than i. If we found any cell with non-zero value, we will return false, otherwise in the end we will return true.

Example

// function to traverse over the matrix
function check(mat){

   // getting total number of rows of matrix
   var rows = mat.length
   
   // getting columns of the given matrix
   var cols = mat[0].length
   
   // traversing over the section present above the main diagonal
   for(var i = 0; i < rows; i++){
      for(var j = i+1; j < cols; j++){
         if(mat[i][j] != 0){
            return false;
         }
      }
   }
   return true;
}

// defining the matrix
var mat = [ [ 1, 0, 0, 0],
   [ 2, 3, 0, 0],
   [4, 5, 6, 0],
   [7, 8, 9, 1]]
   
// given matrix
console.log("The given matrix is: ");
console.log(mat)
if(check(mat)){
   console.log("The given matrix is a lower triangular matrix");
}
else{
   console.log("The given matrix is not a lower triangular matrix");
}

// updating matrix
mat = [ [ 1, 0, 0, 1],
   [ 2, 3, 0, 0],
   [4, 5, 6, 0],
   [7, 8, 9, 1]]
   
// given matrix
console.log("The given matrix is: ");
console.log(mat)
if(check(mat)){
   console.log("The given matrix is a lower triangular matrix");
}
else{
   console.log("The given matrix is not a lower triangular matrix");
}

Output

The given matrix is: 
[ [ 1, 0, 0, 0 ], [ 2, 3, 0, 0 ], [ 4, 5, 6, 0 ], [ 7, 8, 9, 1 ] ]
The given matrix is a lower triangular matrix
The given matrix is: 
[ [ 1, 0, 0, 1 ], [ 2, 3, 0, 0 ], [ 4, 5, 6, 0 ], [ 7, 8, 9, 1 ] ]
The given matrix is not a lower triangular matrix

Time and Space Complexity

The time complexity of the above code is O(N*N), where N is the number of rows of the given matrix. This is because we have traversed over the matrix only once.

The space complexity of the above code is O(1), as we are not using any extra space.

Conclusion

In this tutorial, we have implemented a JavaScript program to find whether the given matrix is a lower triangular matrix or not. A Lower triangular matrix is a squared matrix that has the same number of rows and columns and all the elements that are present above the main diagonal are zero. We have implemented a code to work in O(N*N) time complexity and O(1) space complexity.

Updated on: 13-Apr-2023

66 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements