# Print all the sub diagonal elements of the given square matrix

Given a square matrix **mat[][]** of size **n * n**. The task is to print all the elements which lie on the sub-diagonal of the given matrix.

**Examples:**

Input:mat[][] = {

{1, 2, 3},

{3, 3, 4, },

{2, 4, 6}}

Output:3 4

Input:mat[][] = {

{1, 2, 3, 4},

{3, 3, 4, 4},

{2, 4, 6, 3},

{1, 1, 1, 3}}

Output:3 4 1

**Approach:** The sub-diagonal of a square matrix is the set of elements that lie directly below the elements comprising the main diagonal. As for main diagonal elements, their indexes are like (i = j), for sub-diagonal elements their indexes are as i = j + 1 (i denotes row and j denotes column).

Hence elements **arr[1][0], arr[2][1], arr[3][2], arr[4][3], ….** are the elements of sub-diagonal.

Either traverse all elements of matrix and print only those where i = j + 1 which requires O(n^{2}) time complexity or print traverse only row from 1 to rowCount – 1 and print elements as arr[row][row – 1].

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define R 4 ` `#define C 4 ` ` ` `// Function to print the sub diagonal ` `// elements of the given matrix ` `void` `printSubDiagonal(` `int` `arr[R][C]) ` `{ ` ` ` `for` `(` `int` `i = 1; i < R; i++) { ` ` ` `cout << arr[i][i - 1] << ` `" "` `; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[R][C] = { { 1, 2, 3, 4 }, ` ` ` `{ 5, 6, 7, 8 }, ` ` ` `{ 9, 10, 11, 12 }, ` ` ` `{ 13, 14, 15, 16 } }; ` ` ` ` ` `printSubDiagonal(arr); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `static` `int` `R = ` `4` `; ` `static` `int` `C = ` `4` `; ` ` ` `// Function to print the sub diagonal ` `// elements of the given matrix ` `static` `void` `printSubDiagonal(` `int` `arr[][]) ` `{ ` ` ` `for` `(` `int` `i = ` `1` `; i < R; i++) ` ` ` `{ ` ` ` `System.out.print(arr[i][i - ` `1` `] + ` `" "` `); ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` ` ` `int` `arr[][] = { { ` `1` `, ` `2` `, ` `3` `, ` `4` `}, ` ` ` `{ ` `5` `, ` `6` `, ` `7` `, ` `8` `}, ` ` ` `{ ` `9` `, ` `10` `, ` `11` `, ` `12` `}, ` ` ` `{ ` `13` `, ` `14` `, ` `15` `, ` `16` `} }; ` ` ` ` ` `printSubDiagonal(arr); ` ` ` `} ` `} ` ` ` `// This code is contributed by ajit. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` `R ` `=` `4` `C ` `=` `4` ` ` `# Function to print the sub diagonal ` `# elements of the given matrix ` `def` `printSubDiagonal(arr): ` ` ` ` ` `for` `i ` `in` `range` `(` `1` `, R): ` ` ` `print` `(arr[i][i ` `-` `1` `], end ` `=` `" "` `) ` ` ` `# Driver code ` `arr ` `=` `[[ ` `1` `, ` `2` `, ` `3` `, ` `4` `], ` ` ` `[ ` `5` `, ` `6` `, ` `7` `, ` `8` `], ` ` ` `[ ` `9` `, ` `10` `, ` `11` `, ` `12` `], ` ` ` `[ ` `13` `, ` `14` `, ` `15` `, ` `16` `]] ` ` ` `printSubDiagonal(arr); ` ` ` `# This code is contributed ` `# by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` `class` `GFG ` `{ ` ` ` `static` `int` `R = 4; ` ` ` `static` `int` `C = 4; ` ` ` ` ` `// Function to print the sub diagonal ` ` ` `// elements of the given matrix ` ` ` `static` `void` `printSubDiagonal(` `int` `[,] arr) ` ` ` `{ ` ` ` `for` `(` `int` `i = 1; i < R; i++) ` ` ` `{ ` ` ` `Console.Write(arr[i, i - 1] + ` `" "` `); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `int` `[,]arr = {{ 1, 2, 3, 4 }, ` ` ` `{ 5, 6, 7, 8 }, ` ` ` `{ 9, 10, 11, 12 }, ` ` ` `{ 13, 14, 15, 16 }}; ` ` ` ` ` `printSubDiagonal(arr); ` ` ` `} ` `} ` ` ` `// This code is contributed by CodeMech. ` |

*chevron_right*

*filter_none*

**Output:**

5 10 15

## Recommended Posts:

- Print all the super diagonal elements of the given square matrix
- Program to swap upper diagonal elements with lower diagonal elements of matrix.
- Print matrix in diagonal pattern
- Print numbers in matrix diagonal pattern
- Reverse Diagonal elements of matrix
- Squares of Matrix Diagonal Elements
- Find the sum of the diagonal elements of the given N X N spiral matrix
- Program to convert the diagonal elements of the matrix to 0
- Filling diagonal to make the sum of every row, column and diagonal equal of 3x3 matrix
- Print maximum sum square sub-matrix of given size
- How to access elements of a Square Matrix
- Find a Square Matrix such that sum of elements in every row and column is K
- Row-wise common elements in two diagonals of a square matrix
- Finding the maximum square sub-matrix with all equal elements
- Program to print elements of a Matrix row-wise skipping alternate elements

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.