Posted on by Kalkicode
Code Matrix

Check if matrix is lower triangular

The problem involves checking whether a given matrix is lower triangular or not. A lower triangular matrix is a square matrix in which all the elements above the main diagonal (elements with row index less than column index) are zero.

Problem Statement and Example

Given a matrix, the task is to determine if it's a lower triangular matrix. Consider the following example matrix:

1  0  0  0
1  2  0  0
1  2  3  0
1  2  3  4

In this example, the matrix is indeed lower triangular because all the elements above the main diagonal are zeros.

Idea to Solve

To solve this problem, you need to iterate through each element of the matrix and check if it satisfies the conditions of being a lower triangular matrix. For each element at position (i, j), ensure that all elements with row indices less than i (i.e., elements above the current row) and column indices greater than j (i.e., elements to the right of the current column) are zero.

Pseudocode

function isLowerTriangular(matrix)
    for i = 0 to number_of_rows(matrix) - 1
        for j = i + 1 to number_of_columns(matrix) - 1
            if matrix[i][j] != 0
                return false
    return true

Algorithm Explanation

  1. Loop through each row of the matrix using the variable i.
  2. For each row, loop through the columns starting from i + 1 to the last column using the variable j.
  3. Check if the element at position (i, j) is non-zero. If it's not zero, the matrix can't be lower triangular, so return false.
  4. If all elements above the main diagonal are zero, the matrix is lower triangular, so return true.

Program

/*
  C Program 
+ Check if matrix is lower triangular
*/
#include<stdio.h>
//Size of matrix
#define ROW 4
#define COL 4

//Display the element of given 2d matrix
void show_data(int matrix[][COL]) {

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

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

      printf("%4d", matrix[i][j]);
    }

    printf("\n");
  }

  printf("\n");
}
//Check if given matrix is lower triangular or not
void lower_triangular(int matrix[ROW][COL])
{
  int status=1;
 
  for (int i = 0; i < COL && status==1; ++i)
  {
    for (int j = i+1; j < ROW && status==1; ++j)
    {
      if(matrix[i][j] != 0)
      {
        //When matrix is not lower triangular
        status=0;
      }
    }
    
  }
  if(status==1)
  {
    printf("  Yes\n");
  }
  else
  {
    printf("  NO\n");
  }
 
}
int main(){

  int matrix[ROW][COL]=  { 
    { 1, 0, 0, 0 }, 
    { 1, 2, 0, 0 }, 
    { 1, 2, 3, 0 },
    { 1, 2, 3, 4 }  }; 
  show_data(matrix);
  lower_triangular(matrix);

  return 0;
}

Output

   1   0   0   0
   1   2   0   0
   1   2   3   0
   1   2   3   4

  Yes
/*
  C++ Program
  Check if matrix is lower triangular
*/
#include<iostream>
#define ROW 4
#define COL 4
using namespace std;

class MyMatrix {
	public:

		//Display the element of given 2d matrix
		void show_data(int matrix[][COL], int row, int col) {
			for (int i = 0; i < row; ++i) {
				for (int j = 0; j < col; ++j) {
					cout << " " << matrix[i][j];
				}
				cout << "\n";
			}
			cout << "\n";
		}
	//Check if given matrix is lower triangular or not
	void lower_triangular(int matrix[][COL], int row, int col) {
		int status = 1;
		for (int i = 0; i < col && status == 1; ++i) {
			for (int j = i + 1; j < row && status == 1; ++j) {
				if (matrix[i][j] != 0) {
					//When matrix is not lower triangular
					status = 0;
				}
			}
		}
		if (status == 1) {
			cout << " Yes\n";
		} else {
			cout << " NO\n";
		}
	}
};
int main() {
	MyMatrix obj ;
	int matrix[][COL] = {
		{
			1,
			0,
			0,
			0
		},
		{
			1,
			2,
			0,
			0
		},
		{
			1,
			2,
			3,
			0
		},
		{
			1,
			2,
			3,
			4
		}
	};
	//This is size of matrix

	obj.show_data(matrix, ROW, COL);
	obj.lower_triangular(matrix, ROW, COL);
	return 0;
}

Output

 1 0 0 0
 1 2 0 0
 1 2 3 0
 1 2 3 4

 Yes
/*
  Java Program
  Check if matrix is lower triangular
*/
public class MyMatrix {


  //Display the element of given 2d matrix
  public void show_data(int [][]matrix, int row,int col) {

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

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

        System.out.print("  "+ matrix[i][j]);
      }

      System.out.print("\n");
    }

    System.out.print("\n");
  }
  //Check if given matrix is lower triangular or not
  public void lower_triangular(int [][]matrix, int row,int col)
  {
    int status=1;
   
    for (int i = 0; i < col && status==1; ++i)
    {
      for (int j = i+1; j < row && status==1; ++j)
      {
        if(matrix[i][j] != 0)
        {
          //When matrix is not lower triangular
          status=0;
        }
      }
      
    }
    if(status==1)
    {
      System.out.print("  Yes\n");
    }
    else
    {
      System.out.print("  NO\n");
    }
   
  }

  public static void main(String[] args) 
  {
  

    MyMatrix obj = new MyMatrix();
    int [][]matrix =  { 
      { 1, 0, 0, 0 }, 
      { 1, 2, 0, 0 }, 
      { 1, 2, 3, 0 },
      { 1, 2, 3, 4 }  
    };
    //This is size of matrix
    int row = matrix.length;
    int col = matrix[0].length; 

    obj.show_data(matrix,row,col);
    obj.lower_triangular(matrix,row,col);

  }
}

Output

 1 0 0 0
 1 2 0 0
 1 2 3 0
 1 2 3 4

 Yes
/*
  C# Program
  Check if matrix is lower triangular
*/
using System;

public class MyMatrix {
	//Display the element of given 2d matrix
	public void show_data(int[,] matrix, int row, int col) {
		for (int i = 0; i < row; ++i) {
			for (int j = 0; j < col; ++j) {
				Console.Write(" " + matrix[i,j]);
			}
			Console.Write("\n");
		}
		Console.Write("\n");
	}
	//Check if given matrix is lower triangular or not
	public void lower_triangular(int[,] matrix, int row, int col) {
		int status = 1;
		for (int i = 0; i < col && status == 1; ++i) {
			for (int j = i + 1; j < row && status == 1; ++j) {
				if (matrix[i,j] != 0) {
					//When matrix is not lower triangular
					status = 0;
				}
			}
		}
		if (status == 1) {
			Console.Write(" Yes\n");
		} else {
			Console.Write(" NO\n");
		}
	}
	public static void Main(String[] args) {
		MyMatrix obj = new MyMatrix();
		int[,] matrix = {
			{
				1,
				0,
				0,
				0
			},
			{
				1,
				2,
				0,
				0
			},
			{
				1,
				2,
				3,
				0
			},
			{
				1,
				2,
				3,
				4
			}
		};
		//This is size of matrix
		int row = matrix.GetLength(0);
		int col = matrix.GetLength(1);
		obj.show_data(matrix, row, col);
		obj.lower_triangular(matrix, row, col);
	}
}

Output

 1 0 0 0
 1 2 0 0
 1 2 3 0
 1 2 3 4

 Yes
<?php
/*
  Php Program
  Check if matrix is lower triangular
*/
class MyMatrix {
	//Display the element of given 2d matrix
	public 	function show_data($matrix, $row, $col) {
		for ($i = 0; $i < $row; ++$i) {
			for ($j = 0; $j < $col; ++$j) {
				echo(" ". $matrix[$i][$j]);
			}
			echo("\n");
		}
		echo("\n");
	}
	//Check if given matrix is lower triangular or not
	public 	function lower_triangular($matrix, $row, $col) {
		$status = 1;
		for ($i = 0; $i < $col && $status == 1; ++$i) {
			for ($j = $i + 1; $j < $row && $status == 1; ++$j) {
				if ($matrix[$i][$j] != 0) {
					//When matrix is not lower triangular
					$status = 0;
				}
			}
		}
		if ($status == 1) {
			echo(" Yes\n");
		} else {
			echo(" NO\n");
		}
	}
};

function main() {
	$obj = new MyMatrix();
	$matrix = array(
      array(1, 0, 0, 0), 
      array(1, 2, 0, 0), 
      array(1, 2, 3, 0), 
      array(1, 2, 3, 4)
    );
	//This is size of matrix
	$row = count($matrix);
	$col = count($matrix[0]);
	$obj->show_data($matrix, $row, $col);
	$obj->lower_triangular($matrix, $row, $col);

}
main();

Output

 1 0 0 0
 1 2 0 0
 1 2 3 0
 1 2 3 4

 Yes
/*
  Node Js Program
  Check if matrix is lower triangular
*/
class MyMatrix {
	//Display the element of given 2d matrix
	show_data(matrix, row, col) {
		for (var i = 0; i < row; ++i) {
			for (var j = 0; j < col; ++j) {
				process.stdout.write(" " + matrix[i][j]);
			}

			process.stdout.write("\n");
		}

		process.stdout.write("\n");
	}

	//Check if given matrix is lower triangular or not
	lower_triangular(matrix, row, col) {
		var status = 1;
		for (var i = 0; i < col && status == 1; ++i) {
			for (var j = i + 1; j < row && status == 1; ++j) {
				if (matrix[i][j] != 0) {
					//When matrix is not lower triangular
					status = 0;
				}
			}
		}

		if (status == 1) {
			process.stdout.write(" Yes\n");
		} else {
			process.stdout.write(" NO\n");
		}
	}
}

function main(args) {
	var obj = new MyMatrix();
	var matrix = [
		[1, 0, 0, 0],
		[1, 2, 0, 0],
		[1, 2, 3, 0],
		[1, 2, 3, 4]
	];
	//This is size of matrix
	var row = matrix.length;
	var col = matrix[0].length;
	obj.show_data(matrix, row, col);
	obj.lower_triangular(matrix, row, col);
}

main();

Output

 1 0 0 0
 1 2 0 0
 1 2 3 0
 1 2 3 4

 Yes
#   Python 3 Program
#   Check if matrix is lower triangular
class MyMatrix :
	# Display the element of given 2d matrix
	def show_data(self, matrix, row, col) :
		i = 0
		while (i < row) :
			j = 0
			while (j < col) :
				print(" ", matrix[i][j], end = "")
				j += 1
			
			print("\n", end = "")
			i += 1
		
		print("\n", end = "")
	
	# Check if given matrix is lower triangular or not
	def lower_triangular(self, matrix, row, col) :
		status = 1
		i = 0
		while (i < col and status == 1) :
			j = i + 1
			while (j < row and status == 1) :
				if (matrix[i][j] != 0) :
					# When matrix is not lower triangular
					status = 0
				
				j += 1
			
			i += 1
		
		if (status == 1) :
			print(" Yes\n", end = "")
		else :
			print(" NO\n", end = "")
		
	

def main() :
	obj = MyMatrix()
	matrix = [
		[1, 0, 0, 0],
		[1, 2, 0, 0],
		[1, 2, 3, 0],
		[1, 2, 3, 4]
	]
	row = len(matrix)
	col = len(matrix[0])
	obj.show_data(matrix, row, col)
	obj.lower_triangular(matrix, row, col)


if __name__ == "__main__":
	main()

Output

  1  0  0  0
  1  2  0  0
  1  2  3  0
  1  2  3  4

 Yes
#   Ruby Program
#   Check if matrix is lower triangular
class MyMatrix 
	# Display the element of given 2d matrix
	def show_data(matrix, row, col) 
		i = 0
		while (i < row) 
			j = 0
			while (j < col) 
				print(" ", matrix[i][j])
				j += 1
			end
			print("\n")
			i += 1
		end
		print("\n")
	end
	# Check if given matrix is lower triangular or not
	def lower_triangular(matrix, row, col) 
		status = 1
		i = 0
		while (i < col && status == 1) 
			j = i + 1
			while (j < row && status == 1) 
				if (matrix[i][j] != 0) 
					# When matrix is not lower triangular
					status = 0
				end
				j += 1
			end
			i += 1
		end
		if (status == 1) 
			print(" Yes\n")
		else 
			print(" NO\n")
		end
	end
end
def main() 
	obj = MyMatrix.new()
	matrix = [
		[1, 0, 0, 0],
		[1, 2, 0, 0],
		[1, 2, 3, 0],
		[1, 2, 3, 4]
	]
	row = matrix.length
	col = matrix[0].length
	obj.show_data(matrix, row, col)
	obj.lower_triangular(matrix, row, col)
end
main()

Output

 1 0 0 0
 1 2 0 0
 1 2 3 0
 1 2 3 4

 Yes
/*
  Scala Program
  Check if matrix is lower triangular
*/
class MyMatrix {
	//Display the element of given 2d matrix
	def show_data(matrix: Array[Array[Int]], row: Int, col: Int): Unit = {
		var i: Int = 0;
		while (i < row) {
			var j: Int = 0;
			while (j < col) {
				print(" " + matrix(i)(j));
				j += 1;
			}
			print("\n");
			i += 1;
		}
		print("\n");
	}
	//Check if given matrix is lower triangular or not
	def lower_triangular(matrix: Array[Array[Int]], row: Int, col: Int): Unit = {
		var status: Int = 1;
		var i: Int = 0;
		while (i < col && status == 1) {
			var j: Int = i + 1;
			while (j < row && status == 1) {
				if (matrix(i)(j) != 0) {
					//When matrix is not lower triangular
					status = 0;
				}
				j += 1;
			}
			i += 1;
		}
		if (status == 1) {
			print(" Yes\n");
		} else {
			print(" NO\n");
		}
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		val obj: MyMatrix = new MyMatrix();
		val matrix: Array[Array[Int]] = Array(
			Array(1, 0, 0, 0),
			Array(1, 2, 0, 0),
			Array(1, 2, 3, 0),
			Array(1, 2, 3, 4));
		val row: Int = matrix.length;
		val col: Int = matrix(0).length;
		obj.show_data(matrix, row, col);
		obj.lower_triangular(matrix, row, col);
	}
}

Output

 1 0 0 0
 1 2 0 0
 1 2 3 0
 1 2 3 4

 Yes
/*
  Swift Program
  Check if matrix is lower triangular
*/
class MyMatrix {
	//Display the element of given 2d matrix
	func show_data(_ matrix: [
		[Int]
	], _ row: Int, _ col: Int) {
		var i: Int = 0;
		while (i < row) {
			var j: Int = 0;
			while (j < col) {
				print(" ", matrix[i][j], terminator: "");
				j += 1;
			}
			print("\n", terminator: "");
			i += 1;
		}
		print("\n", terminator: "");
	}
	//Check if given matrix is lower triangular or not
	func lower_triangular(_ matrix: [
		[Int]
	], _ row: Int, _ col: Int) {
		var status: Int = 1;
		var i: Int = 0;
		while (i < col && status == 1) {
			var j: Int = i + 1;
			while (j < row && status == 1) {
				if (matrix[i][j] != 0) {
					//When matrix is not lower triangular
					status = 0;
				}
				j += 1;
			}
			i += 1;
		}
		if (status == 1) {
			print(" Yes\n", terminator: "");
		} else {
			print(" NO\n", terminator: "");
		}
	}
}
func main() {
	let obj: MyMatrix = MyMatrix();
	let matrix: [
		[Int]
	] = [
		[1, 0, 0, 0],
		[1, 2, 0, 0],
		[1, 2, 3, 0],
		[1, 2, 3, 4]
	];
	let row: Int = matrix.count;
	let col: Int = matrix[0].count;
	obj.show_data(matrix, row, col);
	obj.lower_triangular(matrix, row, col);
}
main();

Output

  1  0  0  0
  1  2  0  0
  1  2  3  0
  1  2  3  4

 Yes

Resultant Output Explanation

It iterates through the matrix and checks each element according to the algorithm. If all the conditions are met, it prints "Yes," indicating that the matrix is lower triangular. Otherwise, it prints "NO."

Time Complexity

The time complexity of this algorithm is O(n^2), where 'n' is the number of rows (or columns) in the matrix. This is because we need to iterate through each element of the matrix once and perform a constant amount of work for each element.

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment