In case if you like to visit all questions related to Matrix on our blog,
Lets see algorithm for printing Matrix diagonally.
If we observe the Matrix, there are 2 set of Diagonals,
We will first print the rowCount diagonals and then print the remaining columnCount-1 diagonals.
For printing rowCount diagonals:
For printing (colCount-1) diagonals:
Another way of looping
If we observe the Matrix, there are 2 set of Diagonals,
- rowCount diagonals
- (colCount - 1) diagonals.
We will first print the rowCount diagonals and then print the remaining columnCount-1 diagonals.
For printing rowCount diagonals:
- Iterate "r" from 0 till rowCount
- For each row, initialize "row=r" and "col=0" iterate till (row >=0 && col<collength )
- Print matrix[row][col]
- Decrement row-- and Increment col++
For printing (colCount-1) diagonals:
- Iterate "c" from 1 till colCount
- For each row, initialize "row=rowCount" and "col=c" iterate till (row >=0 && col < collength)
- Print matrix[row][col]
- Decrement row-- and Increment col++
Java Program to Print Matrix Diagonally.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | package javabypatel.matrix; /* Input: {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}, {17, 18, 19, 20}, Output: 1 5 2 9 6 3 13 10 7 4 17 14 11 8 18 15 12 19 16 20 */ public class PrintMatrixDiagonally { public static void main(String[] args) { new PrintMatrixDiagonally(); } public PrintMatrixDiagonally() { int [][] matrix = { { 1 , 2 , 3 , 4 }, { 5 , 6 , 7 , 8 }, { 9 , 10 , 11 , 12 }, { 13 , 14 , 15 , 16 }, { 17 , 18 , 19 , 20 } }; printMatrixDiagonally(matrix); } private void printMatrixDiagonally( int [][] matrix){ int rowCount = matrix.length; int columnCount = matrix[ 0 ].length; for ( int r = 0 ; r < rowCount; r++) { for ( int row = r, col = 0 ; row >= 0 && col < columnCount; row--, col++) { System.out.print(matrix[row][col] + " " ); } System.out.println(); } for ( int c = 1 ; c < columnCount; c++) { for ( int row = rowCount- 1 , col = c; row >= 0 && col < columnCount; row--, col++) { System.out.print(matrix[row][col] + " " ); } System.out.println(); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | package javabypatel.matrix; public class PrintMatrixDiagonally { public static void main(String[] args) { int [][] matrix = { { 1 , 2 , 3 , 4 }, { 5 , 6 , 7 , 8 }, { 9 , 10 , 11 , 12 }, { 13 , 14 , 15 , 16 }, { 17 , 18 , 19 , 20 } }; printMatrixDiagonally(matrix); } private static void printMatrixDiagonally( int [][] matrix) { for ( int i = 0 ; i < matrix.length ; i++) { int row = i; int col = 0 ; while (row >= 0 && col < matrix[ 0 ].length) { System.out.print(matrix[row][col] + " " ); col++; row--; } System.out.println(); } for ( int i = 1 ; i < matrix[ 0 ].length ; i++) { int row = matrix.length- 1 ; int col = i; while (row >= 0 && col < matrix[ 0 ].length) { System.out.print(matrix[row][col] + " " ); col++; row--; } System.out.println(); } } } |
Compress a given string in-place and with constant extra space.
Check whether a given string is an interleaving of String 1 and String 2.
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord.
Serialize and Deserialize a Binary Tree
Advanced Multithreading Interview Questions In Java
Enjoy !!!!
If you find any issue in post or face any error while implementing, Please comment.