Open In App

Program to check if matrix is upper triangular

Last Updated : 07 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a square matrix mat[][], the task is to determine whether it is in upper triangular form. A matrix is considered upper triangular if all elements below the main diagonal are zero, while the diagonal and elements above it can be any value.

Examples: 

Input: mat[][] = [[1, 2, 3]
[0, 5, 6]
[0, 0, 9]]
Output: Yes
Explanation: All elements below the main diagonal are 0, so the matrix is upper triangular.

Input: mat[][] = [[3, 2, 6, 7]
[0, 6, 6, 8]
[0, 0, 2, 4]
[0, 0, 0, 1]]
Output: Yes
Explanation: All elements below the main diagonal are 0, so the matrix is upper triangular.

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

Approach:

The idea is to loop through the lower half of the matrix (i.e., cells where row index i > column index j) and verify if each such cell contains zero. The observation that only elements below the diagonal affect whether a matrix is upper triangular. if any one of those elements is non-zero, we can immediately return false, indicating the matrix is not upper triangular.

C++
// C++ Code to check if a matrix is
// upper triangular matrix
#include <iostream>
#include <vector>
using namespace std;

// Function to check whether the matrix
// is upper triangular
bool isUpperTriangular(vector<vector<int>> mat) {

    int n = mat.size();
    
    // Traverse all elements below the main diagonal
    for (int i = 1; i < n; i++) {

        for (int j = 0; j < i; j++) {

            // Check if any element below main 
            // diagonal is non-zero
            if (mat[i][j] != 0) {

                return false;
            }
        }
    }

    return true;
}

// Driver code
int main() {

    vector<vector<int>> mat = {{1, 2, 3},
                               {0, 5, 6},
                               {0, 0, 9}};

    if (isUpperTriangular(mat)) {
        cout << "Yes";
    } else {
        cout << "No";
    }

    return 0;
}
Java
// Java Code to check if a matrix is
// upper triangular matrix
class GfG {

    // Function to check whether the matrix
    // is upper triangular
    static boolean isUpperTriangular(int[][] mat) {

        int n = mat.length;
        
        // Traverse all elements below the main diagonal
        for (int i = 1; i < n; i++) {

            for (int j = 0; j < i; j++) {

                // Check if any element below main 
                // diagonal is non-zero
                if (mat[i][j] != 0) {

                    return false;
                }
            }
        }

        return true;
    }

    // Driver code
    public static void main(String[] args) {

        int[][] mat = {{1, 2, 3},
                       {0, 5, 6},
                       {0, 0, 9}};

        if (isUpperTriangular(mat)) {
            System.out.print("Yes");
        } else {
            System.out.print("No");
        }
    }
}
Python
# Python Code to check if a matrix is
# upper triangular matrix

# Function to check whether the matrix
# is upper triangular
def isUpperTriangular(mat):

    n = len(mat)
    
    # Traverse all elements below the main diagonal
    for i in range(1, n):

        for j in range(i):

            # Check if any element below main 
            # diagonal is non-zero
            if mat[i][j] != 0:

                return False

    return True

# Driver code
if __name__=="__main__":

    mat = [[1, 2, 3],
           [0, 5, 6],
           [0, 0, 9]]

    if isUpperTriangular(mat):
        print("Yes")
    else:
        print("No")
C#
// C# Code to check if a matrix is
// upper triangular matrix
using System;

class GfG {

    // Function to check whether the matrix
    // is upper triangular
    static bool isUpperTriangular(int[,] mat) {

        int n = mat.GetLength(0);
        
        // Traverse all elements below the main diagonal
        for (int i = 1; i < n; i++) {

            for (int j = 0; j < i; j++) {

                // Check if any element below main 
                // diagonal is non-zero
                if (mat[i, j] != 0) {

                    return false;
                }
            }
        }

        return true;
    }

    // Driver code
    static void Main() {

        int[,] mat = {{1, 2, 3},
                      {0, 5, 6},
                      {0, 0, 9}};

        if (isUpperTriangular(mat)) {
            Console.Write("Yes");
        } else {
            Console.Write("No");
        }
    }
}
JavaScript
// JavaScript Code to check if a matrix is
// upper triangular matrix

// Function to check whether the matrix
// is upper triangular
function isUpperTriangular(mat) {

    let n = mat.length;
    
    // Traverse all elements below the main diagonal
    for (let i = 1; i < n; i++) {

        for (let j = 0; j < i; j++) {

            // Check if any element below main 
            // diagonal is non-zero
            if (mat[i][j] !== 0) {

                return false;
            }
        }
    }

    return true;
}

// Driver code
let mat = [[1, 2, 3],
           [0, 5, 6],
           [0, 0, 9]];

if (isUpperTriangular(mat)) {
    console.log("Yes");
} else {
    console.log("No");
}

Output
Yes

Time Complexity: O(n2), where n represents the number of rows and columns of the matrix.
Space Complexity: O(1), no extra space is required, so it is a constant.


Next Article
Practice Tags :

Similar Reads