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