![]() |
VOOZH | about |
A matrix is a rectangular arrangement of elements in rows and columns. In programming, it is implemented as a 2-D array, where each element is accessed using two indices [row][column].
Traversing a Matrix (2-D array) means visiting every element using nested loops. Since a matrix has rows and columns, traversal requires:
1 2 3 4 5 6 1 4 2 5 3 6
1. Matrix Multiplication
Matrix multiplication is a classical matrix problem where each element of the resulting matrix is calculated as the dot product of a row from the first matrix and a column from the second. It combines two matrices to form a new one based on this rule.
Core Idea: For C[i][j], sum the product of corresponding elements from row i of matrix A and column j of matrix B:
C[i][j] = A[i][0]*B[0][j] + A[i][1]*B[1][j] + ... + A[i][k]*B[k][j]
2. Rotate Square Matrix by 90 Degrees
Rotate Square Matrix by 90 Degrees is a classical problem where a square 2D matrix is rotated 90 degrees clockwise. In this transformation, the first row becomes the last column, the second row becomes the second-last column, and so on.
3. Transpose of a Matrix
Transpose of a matrix is a classical matrix problem that involves flipping the matrix over its main diagonal, converting rows into columns. The element at matrix[i][j] is moved to matrix[j][i].
Core Idea: Swap elements matrix[i][j] and matrix[j][i] for all i < j to flip the matrix over its main diagonal without using extra space.
4. Spiral Traversal
Spiral traversal is the classical matrix problem where elements are accessed in a spiral order starting from the top-left corner.The traversal moves right → down → left → up layer by layer until all elements are visited.
Core Idea: Traverse the matrix layer by layer, adjusting boundaries to follow a directional pattern, ensuring no element is visited twice.