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(n2) 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++ 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;
} |
// 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. |
# 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 |
// 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. |
<script> // Javascript implementation of the approach var R = 4
var C = 4
// Function to print the sub diagonal // elements of the given matrix function printSubDiagonal(arr)
{ for ( var i = 1; i < R; i++) {
document.write( arr[i][i - 1] + " " );
}
} // Driver code var arr = [ [ 1, 2, 3, 4 ],
[ 5, 6, 7, 8 ],
[ 9, 10, 11, 12 ],
[ 13, 14, 15, 16 ] ];
printSubDiagonal(arr); </script> |
5 10 15
Time complexity: O(R) where R is number of rows of given matrix
Auxiliary space: O(1)
Another Approach:
Initialize two variables, i and j, to zero.
Traverse the matrix from the second row to the last row and from the first column to the second last column:
a. Increment i and j to move to the next sub diagonal element.
b. Print the sub diagonal element at index (i, j).
Continue the traversal until the last sub diagonal element is printed.
// C++ implementation of the program #include <iostream> using namespace std;
int main() {
int mat[4][4] = {
{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16} }; int n = 4;
// Traverse the sub diagonals and print the elements for ( int k = 1; k < n; k++) {
int i = k;
int j = 0;
while (i < n && j < n) {
cout << mat[i][j] << " " ;
i++;
j++;
}
} return 0;
} |
// Javascript implementation of the program #include <stdio.h> int main() {
int mat[4][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}
};
int n = 4;
// Traverse the sub diagonals and print the elements
for ( int k = 1; k < n; k++) {
int i = k;
int j = 0;
while (i < n && j < n) {
printf ( "%d " , mat[i][j]);
i++;
j++;
}
}
return 0;
} |
import java.util.*;
public class Main {
public static void main(String[] args) {
int [][] mat = {
{ 1 , 2 , 3 , 4 },
{ 5 , 6 , 7 , 8 },
{ 9 , 10 , 11 , 12 },
{ 13 , 14 , 15 , 16 }
}; int n = 4 ;
// Traverse the sub diagonals and print the elements
for ( int k = 1 ; k < n; k++) {
int i = k;
int j = 0 ;
while (i < n && j < n) {
System.out.print(mat[i][j] + " " );
i++;
j++;
}
}
} } |
mat = [
[ 1 , 2 , 3 , 4 ],
[ 5 , 6 , 7 , 8 ],
[ 9 , 10 , 11 , 12 ],
[ 13 , 14 , 15 , 16 ]
] n = 4
# Traverse the sub diagonals and print the elements for k in range ( 1 , n):
i = k
j = 0
while i < n and j < n:
print (mat[i][j], end = " " )
i + = 1
j + = 1
# This code is contributed By - Dwaipayan Bandyopadhyay |
using System;
class Program {
static void Main()
{
int [, ] mat = new int [4, 4] { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
int n = 4;
// Traverse the sub diagonals and print the elements
for ( int k = 1; k < n; k++) {
int i = k;
int j = 0;
while (i < n && j < n) {
Console.Write(mat[i, j] + " " );
i++;
j++;
}
}
// Add this line to keep the output visible before
// the program exits
Console.WriteLine();
// Optionally, wait for user input to see the output
// before the program exits
Console.ReadLine();
}
} |
// JavaScript implementation of the program // Define a 4x4 matrix let mat = [ [1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]
]; let n = 4; // Traverse the sub diagonals and print the elements for (let k = 1; k < n; k++) {
let i = k;
let j = 0;
while (i < n && j < n) {
console.log(mat[i][j] + " " );
i++;
j++;
}
} |
5 10 15 9 14 13
Time complexity : O(n^2), where n is the size of the matrix
Space complexity : O(1)