http://www.ideserve.co.in/learn/print-matrix-diagonally
Given a matrix of mxn dimensions, print the elements of the matrix in diagonal order.
Then, number of diagonals will be = rowCount + columnCount - 1 as depicted in the diagram below.
Given a matrix of mxn dimensions, print the elements of the matrix in diagonal order.
Then, number of diagonals will be = rowCount + columnCount - 1 as depicted in the diagram below.
public static void printMatrixDiagonally(int[][] matrix) {
int row, col;
int rowCount = matrix.length;
int columnCount = matrix[0].length;
for
(int k = 0; k < rowCount; k++) {
for
(row = k, col = 0; row >= 0 && col < columnCount; row--, col++) {
System.out.print(matrix[row][col] +
" "
);
}
System.out.println();
}
for
(int k = 1; k < columnCount; k++) {
for
(row = rowCount - 1, col = k; row >= 0 && col < columnCount; row--, col++) {
System.out.print(matrix[row][col] +
" "
);
}
System.out.println();
}
}