Diagonals Through an Element
EasyP3Coding
matrix contains the value k exactly once, at some cell (r, c). Return two arrays:
- The primary diagonal through that cell: all cells
(i, j)withi - j == r - c, listed from top-left to bottom-right (increasing row). - The secondary diagonal through that cell: all cells
(i, j)withi + j == r + c, listed from top-right to bottom-left (increasing row).
For [[1,2,3],[4,5,6],[7,8,9]] and k = 6 the result is [[2,6],[6,8]].
Example 1
- Input
- matrix = [[1,2,3],[4,5,6],[7,8,9]], k = 6
- Output
- [[2,6],[6,8]]
Example 2
- Input
- matrix = [[1,2,3],[4,5,6],[7,8,9]], k = 5
- Output
- [[1,5,9],[3,5,7]]
Example 3
- Input
- matrix = [[1,2,3],[4,5,6],[7,8,9]], k = 1
- Output
- [[1,5,9],[1]]
Constraints
- 1 <= rows, cols <= 100
- All values in the matrix are distinct.
- k appears in the matrix.
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 10. ⌘Enter runs, ⌘⇧Enter submits.