Efficient method to store a Lower Triangular Matrix using row | Java program for the above approach ; Dimensions of a matrix ; Structure of the efficient matrix ; Function to set the values in the Matrix ; Function to store the values in the Matrix ; Function to display the elements of the matrix ; Traverse the matrix ; Function to generate an efficient matrix ; Declare efficient Matrix ; Initialize the Matrix ; Set the values in matrix ; Return the matrix ; Driver Code ; Stores the efficient matrix ; Print the Matrix
class GFG { static int N = 5 ; static class Matrix { int [ ] A ; int size ; } ; static void Set ( Matrix mat , int i , int j , int x ) { if ( i >= j ) mat . A [ i * ( i - 1 ) / 2 + j - 1 ] = x ; } static int Get ( Matrix mat , int i , int j ) { if ( i >= j ) { return mat . A [ i * ( i - 1 ) / 2 + j - 1 ] ; } else { return 0 ; } } static void Display ( Matrix mat ) { int i , j ; for ( i = 1 ; i <= mat . size ; i ++ ) { for ( j = 1 ; j <= mat . size ; j ++ ) { if ( i >= j ) { System . out . printf ( " % d ▁ " , mat . A [ i * ( i - 1 ) / 2 + j - 1 ] ) ; } else { System . out . printf ( "0 ▁ " ) ; } } System . out . printf ( "NEW_LINE"); } } static Matrix createMat ( int Mat [ ] [ ] ) { Matrix mat = new Matrix ( ) ; mat . size = N ; mat . A = new int [ ( mat . size * ( mat . size + 1 ) ) / 2 ] ; int i , j ; for ( i = 1 ; i <= mat . size ; i ++ ) { for ( j = 1 ; j <= mat . size ; j ++ ) { Set ( mat , i , j , Mat [ i - 1 ] [ j - 1 ] ) ; } } return mat ; } public static void main ( String [ ] args ) { int Mat [ ] [ ] = { { 1 , 0 , 0 , 0 , 0 } , { 1 , 2 , 0 , 0 , 0 } , { 1 , 2 , 3 , 0 , 0 } , { 1 , 2 , 3 , 4 , 0 } , { 1 , 2 , 3 , 4 , 5 } } ; Matrix mat = createMat ( Mat ) ; Display ( mat ) ; } }