In a compact single dimensional array representation for lower triangular matrices (i.e all the elements above the diagonal are zero) of size , non-zero elements, (i.e elements of lower triangle) of each row are stored one after another, starting from the first row, the index of the element of the lower triangular matrix in this new representation is:
View worked solution
Answer: C
To find the index of the element of a lower triangular matrix in a compact 1D array representation, we can count the number of elements stored before it.
Assuming 1-based indexing for the matrix coordinates and 0-based indexing for the 1D array:
- Elements in preceding rows:
Since it is a lower triangular matrix, row contains exactly elements. Therefore, the total number of elements in all rows preceding row (from row to ) is:
- Elements in the current row :
In row , the element is at column . Since columns are 1-indexed, there are elements in row that lie before the element.
Adding these two values gives the total number of elements stored before the element:
In a 0-indexed 1D array, the index of an element is exactly equal to the number of elements preceding it. Thus, the index is:
Example Verification:
Consider a lower triangular matrix stored in a 0-indexed array:
- Row 1: (Index 0)
- Row 2: (Index 1), (Index 2)
- Row 3: (Index 3), (Index 4), (Index 5)
For element (where ):
This matches the actual index 4 in our representation.
Free practice · Your answers on this page are not saved.
