# Print all the super 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 super-diagonal of the given matrix.

Examples:

Input: mat[][] = {
{1, 2, 3},
{3, 3, 4, },
{2, 4, 6}}
Output: 2 4

Input: mat[][] = {
{1, 2, 3, 4},
{3, 3, 4, 4},
{2, 4, 6, 3},
{1, 1, 1, 3}}
Output: 2 4 3

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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

Hence elements arr, arr, arr, arr, …. are the elements of super-diagonal.

Either traverse all elements of matrix and print only those where j = i + 1 which requires O(n2) time complexity or traverse only column from 1 to columnCount – 1 and print elements as arr[column – 1][column].

Below is the implementation of the above approach:

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

 `// 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 < C; i++)  ` `    ``{  ` `            ``System.out.print(arr[i-``1``][i] + ``" "``);  ` `    ``}  ` `}  ` ` `  `// 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 mohit kumar 29  `

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

 `// C# implementation of the approach  ` `using` `System; ` `     `  `lass 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 < C; i++)  ` `        ``{  ` `                ``Console.Write(arr[i-1,i] + ``" "``);  ` `        ``}  ` `    ``}  ` ` `  `    ``// 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 PrinciRaj1992 */`

Output:
```2 7 12
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

Discovering ways to develop a plane for soaring career goals

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.

Article Tags :
Practice Tags :