Given the following matrix:
1 2 3
4 5 6
7 8 9
I would have to be traversed in the following order:
[3],[2,6],[1,5,9],[4,8],[7]
I have written the code for traversing in the order of
[1],[2,4],[3,5,7],[6,8],[9]
It works well:
int rows = output.length;
int cols = output[0].length;
int maxSum = rows + cols - 2;
for (int sum = 0; sum <= maxSum; sum++) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (i + j - sum == 0) {
System.out.print(output[i][j] + "\t");
}
}
}
}
How can i modify this code to achieve the order that I need? How can i achieve the right diagonally traversing a matrix?
Hows about this in your inner loop ?
System.out.print(output[i][output[0].length-j-1] + "\t");