Check If Matrix Is Upper Triangular in JavaScript



To check if matrix is upper triangular matrix, we check if all the elements below the main diagonal is 0. We are going to discuss two different approaches and compare their complexities.

In this article we are having a 2D square matrix, our task is to write a JavaScript program to check if matrix is upper triangular matrix. Users must be familiar with upper triangular matrix, nested for loop, conditional statement and javascript methods.

Upper triangular matrix

Example

Input:
Matrix:  
    [[1, 2, 3, 4],
    0, 5, 6, 7],
    [0, 0, 8, 9],
    [0, 0, 0, 1]]

Output: true, it is upper triangular matrix

Input:
Matrix:  
    [[1, 2, 3, 4],
    [0, 5, 6, 7],
    [0, 0, 8, 9],
    [0, 1, 0, 1]]

Output: false, it is not upper triangular matrix

Approaches to Check Upper Triangular Matrix

Here is a list of approaches to check if matrix is upper triangular which we will be discussing in this article with stepwise explaination and complete example codes.

Using Nested for Loop

To check if matrix is upper triangular matrix, we have used nested for loop that iterates over rows and column to check the presence of non zero element below main diagonal of the matrix.

  • We have declared two 2D arrays as matrix1 and matrix2, and defined a function check() that takes matrix as argument.
  • Inside check() function, first we get the size of the matrix using length property and then used nested for loop to iterate over rows and columns of matrix.
  • The outer for loop iterates over rows of the matrix while inner for loop iterates over column of the matrix.
  • The inner loop checks for the element value as 0 below main diagonal (j if/else statement. It returns false if matrix value is non-zero below main diagonal.
  • It returns true after execution of loop if non-zero element is not found below diagonal.
  • It uses if/else statement to check the output of check() function and display the result accordingly in web console using console.log().

Example

Here is a complete example code implementing above mentioned steps to check if matrix is upper triangular using nested for loop.

function check(matrix) {
    let n = matrix.length;
    for(var i = 1; i 


Using every() with slice() Function

In this approach to check if matrix is upper triangular matrix we have used every() method with slice() method.

  • We have declared a 2D arrays as matrix and defined a function check() that takes matrix as argument.
  • Inside check() first we check whether given matrix is square or not. Then we have returned an expression which checks for upper triangular matrix.
  • The slice(1) skips the first row and we have used every() method for the remaining row. The row.slice(0, i + 1) gets all the elements below the diagonal element where, i represent index in sliced matrix.
  • Then we have checked if elements below the main diagonal is zero or not using every(value => value === 0).
  • Then the result either, true or false is printed in web console using console.log() method.

Example

Here is a complete example code implementing above mentioned steps to check if matrix is upper triangular using every() method with slice() method.

function check(matrix) {
    let n = matrix.length;
    if (n !== matrix[0].length) 
        return false; 

    return matrix.slice(1).every((row, i) =>
        row.slice(0, i + 1).every(value => value === 0)
    );
}
const matrix = [
    [1, 2, 3, 4],
    [0, 5, 6, 7],
    [0, 0, 8, 9],
    [0, 0, 0, 1]
];
console.log("Is the given matrix an upper triangular matrix?: ")
console.log(check(matrix));

Complexity Comparison

Here is a comparison of time and space complexity of all the above approaches.

Approach Time Complexity Space Complexity
Nested for loop O(n^2) O(1)
every() with slice() O(n^2) O(1)

Conclusion

In this article to write a Javascript program to check if matrix is upper triangular matrix, we have used two different approaches. These approaches are: by using nested for loop and using slice() with every() method.

Practice and learn from a wide range of JavaScript examples, including event handling, form validation, and advanced techniques. Interactive code snippets for hands-on learning.
Updated on: 2024-12-12T11:47:05+05:30

160 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements