Consider a n × n tri-diagonal sparse matrix A. Suppose we want to store sparse A in a one-dimensional array. What is the index of the (i,j)th element of A…
Consider a n × n tri-diagonal sparse matrix A. Suppose we want to store sparse A in a one-dimensional array. What is the index of the (i,j)th element of A in the one-dimensional array?
Show answerHide answer
Correct answer: (B) 2i + j − 2
Explanation
Row-major with 1-based indexes, rows before row i hold 3(i−1) − 1 elements, and (i, j) is the (j − i + 2)th element of row i. Total = 3i − 4 + j − i + 2 = 2i + j − 2.