PIXELBANKv9.1.0
Menu

Given an m x n matrix, return all elements in diagonal order (alternating up-right and down-left).

Output space-separated.

Example:

Input:
1,2,3
4,5,6
7,8,9
Output:
1 2 4 7 5 3 6 8 9
Reasoning:
  • The input matrix is traversed in diagonal order, starting from the top-left corner and alternating between up-right and down-left directions.
  • The first diagonal consists of a single element: 11.
  • The next diagonal consists of elements 22 and 44, and then 77 is added as it is the next element in the down-left diagonal.
  • The following diagonals are traversed in the same manner, resulting in the sequence 1,2,4,7,5,3,6,8,91, 2, 4, 7, 5, 3, 6, 8, 9.
  • The final output is the space-separated sequence of elements in diagonal order: 1247536891 2 4 7 5 3 6 8 9

Constraints:

  • 1 <= m, n <= 10^4
  • 1 <= m * n <= 10^4
solution.py

Test Results

0/0
Run code to see test results.
Diagonal Traverse - Medium | PixelBank