#K50512. Matrix Diagonals Extraction

    ID: 28881 Type: Default 1000ms 256MiB

Matrix Diagonals Extraction

Matrix Diagonals Extraction

Given a matrix represented as a 2D collection of integers, your task is to extract and print its diagonals. Each diagonal is formed by starting from one of the matrix's left-most or bottom-most elements, and progressing in the \(\text{up-right}\) direction (i.e. moving one step up and one step to the right) until you hit a boundary.

Formally, if the matrix has \(m\) rows and \(n\) columns, then there are \(m+n-1\) diagonals. For a diagonal indexed by \(k\) (with \(0 \le k < m+n-1\)), if \(k < m\) the starting element is at position \((k, 0)\); otherwise, the starting element is at position \((m-1, k-m+1)\). From this starting position, repeatedly move to \((i-1, j+1)\) until you go outside the matrix boundaries.

inputFormat

The input is read from stdin and consists of:

  • A line containing two space-separated integers, \(m\) and \(n\), representing the number of rows and columns of the matrix.
  • Following that, there are \(m\) lines, each containing \(n\) space-separated integers representing the matrix row-by-row.

outputFormat

Output all the diagonals to stdout, one per line. In each line, output the elements of that diagonal separated by a single space. If the matrix is empty (i.e. if \(m = 0\) or \(n = 0\)), output nothing.

## sample
3 3
1 2 3
4 5 6
7 8 9
1

4 2 7 5 3 8 6 9

</p>