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
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
Показать
Комментарии отсутствуют
Информация о видео
Другие видео канала
Programming Interview Question: How to print all diagonal's sums for a given binary tree?Level Order TraversalReverse a Linked List - IterativeProgramming Interview Question: Searching a 2D Sorted MatrixImplement a fair coin given an unfair coinBuying and selling stocksBinary SearchMinimum length subarray of an unsorted array sorting which results in complete sorted arrayFind an element in a sorted rotated array without finding pivot (minimum element)Maximum size square sub-matrix with all 1sBuilding Bridges Dynamic ProgrammingLeaders in an arrayFind intersection of two Linked Lists - O(A + B) Time Complexity and O(1) Space ComplexityCreate a balanced Binary Search Tree (BST) from a sorted arrayNext greater element in an arraySpiral level order traversal of a binary treeProgramming Interview Question: Recover Binary Search TreeDetect a loop in a linked listProgramming Interview Question: Find intersection of two Linked ListsFind an element in a sorted rotated arrayLowest Common Ancestor