Print Matrix Diagonally OR Diagonal order of Matrix.

Print Matrix Diagonally. OR
Loop diagonally through two dimensional array. OR
Traverse an array diagonally. OR
Print elements of Matrix in Diagonal order.


Given a Matrix / 2D array, Print all elements of Matrix in Diagonal order. Lets understand Diagonal order of Matrix graphically.

Algorithm


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,
  1. rowCount diagonals
  2. (colCount - 1) diagonals.

We will first print the rowCount diagonals and then print the remaining columnCount-1 diagonals.



For  printing rowCount diagonals:  
  1. Iterate "r" from 0 till rowCount
  2. For each row, initialize "row=r" and "col=0" iterate till (row >=0 && col<collength )
  3. Print matrix[row][col]
  4. Decrement row-- and Increment col++

For  printing (colCount-1) diagonals:  
  1. Iterate "c" from 1 till colCount
  2. For each row, initialize "row=rowCount" and "col=c" iterate till (row >=0 && col < collength)
  3. Print matrix[row][col]
  4. Decrement row-- and Increment col++


Java Program to Print Matrix Diagonally.


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();
  }

 }
 
}


Another way of looping
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.

Post a Comment