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.

What is a Lower Triangular Matrix?

A lower triangular matrix is a square matrix where all elements above the main diagonal are zero. The main diagonal runs from the top-left corner to the bottom-right corner of the matrix.

1 0 0 0 2 3 0 0 4 5 6 0 7 8 9 1 Upper Triangle (All zeros) Lower Triangle (Can have any values) Main Diagonal

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 have the value zero.

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

Explanation: We can see that the main diagonal contains the elements 1, 3, 6, and 1 but all the cells present above the main diagonal don't have the value zero (element at position [0][3] is 1).

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.

Implementation

// 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

How It Works

The algorithm checks only the upper triangular portion of the matrix. For a matrix with n rows and n columns, we iterate through each row i and check columns j where j > i. This ensures we only examine elements above the main diagonal. If any element is non-zero, the matrix is not lower triangular.

Time and Space Complexity

The time complexity of the above code is O(N²), where N is the number of rows of the given matrix. This is because we traverse approximately half of the matrix elements (upper triangle).

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

Conclusion

In this tutorial, we implemented a JavaScript program to check if a given matrix is lower triangular. The solution efficiently checks only the upper triangle elements and returns false immediately upon finding a non-zero element above the main diagonal.

Updated on: 2026-03-15T23:19:01+05:30

192 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements