Загрузка страницы

Print Matrix Diagonally

Given a matrix of mxn dimensions, print the elements of the matrix in diagonal order.

Algorithm:
rowCount = number of rows
columnCount = number of columns
Then, number of diagonals will be = rowCount + columnCount - 1

Step 1: Print first rowCount diagonals
Print diagonals that start from the first column
elements.

Step 2: Print next columnCount - 1 diagonals
Print diagonals that start from the last row
elements.

Step 1 Details: Print first rowCount diagonals
Iterate to print diagonals from row k = 0 to rowCount - 1.
1: Start with row = k and col = 0
2: Print the element matrix[row][col]
3: Decrement row by 1 Increment col by 1till row greater than or equal to 0 and col less than columnCount

Step 2 Details: Print next columnCount – 1 diagonals
Iterate to print diagonals from column k = 1 to columnCount - 1
1: Start with last row, row = rowCount – 1 and col = k
2: Print the element matrix[row][col]
3: Decrement row by 1 Increment col by 1till row greater than or equal to 0 and col less than columnCount

Order of the Algorithm:
Time Complexity: O(mn)
Space Complexity: O(1)

Code and Algorithm Visualization:
http://www.ideserve.co.in/learn/print-matrix-diagonally

Website: http://www.ideserve.co.in

Facebook: https://www.facebook.com/IDeserve.co.in

Видео Print Matrix Diagonally канала IDeserve
Показать
Комментарии отсутствуют
Введите заголовок:

Введите адрес ссылки:

Введите адрес видео с YouTube:

Зарегистрируйтесь или войдите с
Информация о видео
23 января 2016 г. 11:13:36
00:07:35
Яндекс.Метрика