Skip to content
Official PYQ · 1994

GATE CS 1994 Q1.11 — Arrays, Stacks and Queues

Explain with AI

In a compact single dimensional array representation for lower triangular matrices (i.e all the elements above the diagonal are zero) of size n×nn \times n, 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 (i,j)th(i, j)^{th} element of the lower triangular matrix in this new representation is:

Choose one option
View worked solution

Answer: C

To find the index of the (i,j)th(i, j)^{\text{th}} 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 (i,j)(i, j) and 0-based indexing for the 1D array:

  1. Elements in preceding rows:

    Since it is a lower triangular matrix, row kk contains exactly kk elements. Therefore, the total number of elements in all rows preceding row ii (from row 11 to i−1i-1) is:

    1+2+3+⋯+(i−1)=i(i−1)21 + 2 + 3 + \dots + (i-1) = \frac{i(i-1)}{2}
  2. Elements in the current row ii:

    In row ii, the element is at column jj. Since columns are 1-indexed, there are j−1j-1 elements in row ii that lie before the (i,j)th(i, j)^{\text{th}} element.

Adding these two values gives the total number of elements stored before the (i,j)th(i, j)^{\text{th}} element:

Total preceding elements=i(i−1)2+(j−1)\text{Total preceding elements} = \frac{i(i-1)}{2} + (j-1)

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:

(j−1)+i(i−1)2(j-1) + \frac{i(i-1)}{2}

Example Verification:

Consider a 3×33 \times 3 lower triangular matrix stored in a 0-indexed array:

  • Row 1: A1,1A_{1,1} (Index 0)
  • Row 2: A2,1A_{2,1} (Index 1), A2,2A_{2,2} (Index 2)
  • Row 3: A3,1A_{3,1} (Index 3), A3,2A_{3,2} (Index 4), A3,3A_{3,3} (Index 5)

For element A3,2A_{3,2} (where i=3,j=2i=3, j=2):

Index=(2−1)+3(3−1)2=1+3=4\text{Index} = (2-1) + \frac{3(3-1)}{2} = 1 + 3 = 4

This matches the actual index 4 in our representation.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Arrays, Stacks and Queues questions