Diagonal Traverse II - Problem
Diagonal Traverse II presents you with a fascinating 2D array traversal challenge! Given a 2D integer array nums (which may have rows of different lengths - a jagged array), your task is to traverse all elements diagonally and return them in a single array.

Unlike a regular matrix, this problem deals with jagged arrays where each row can have a different number of elements. You need to collect elements along each diagonal line, starting from the top-left and moving towards the bottom-right.

Key insight: Elements on the same diagonal share the property that their row + column indices are equal! For example, elements at positions (0,0), (1,0), (0,1) would be on different diagonals with sums 0, 1, and 1 respectively.

Goal: Return all elements traversed diagonally, where within each diagonal, elements are processed from top to bottom.

Input & Output

example_1.py — Python
$ Input: nums = [[1,2,3],[4,5],[6]]
Output: [1,2,4,3,5,6]
💡 Note: Diagonal 0: [1] (sum=0), Diagonal 1: [2,4] (sum=1), Diagonal 2: [3,5,6] (sum=2). Concatenating gives [1,2,4,3,5,6].
example_2.py — Python
$ Input: nums = [[1,2,3,4,5],[6,7],[8],[9,10,11],[12,13,14,15,16]]
Output: [1,6,8,9,12,2,7,10,13,3,4,11,14,15,5,16]
💡 Note: Elements are grouped by diagonal sum: D0:[1], D1:[6,8,9,12], D2:[2,7,10,13], D3:[3,4,11,14,15], D4:[5,16]. Within each diagonal, elements appear in row order (top to bottom).
example_3.py — Python
$ Input: nums = [[1,2],[3,4,5,6]]
Output: [1,3,2,4,5,6]
💡 Note: Simple case with two rows. D0:[1], D1:[3,2], D2:[4,5], D3:[6]. Note how elements from different rows can be on the same diagonal.

Constraints

  • 1 ≤ nums.length ≤ 105
  • 1 ≤ nums[i].length ≤ 105
  • 1 ≤ sum(nums[i].length) ≤ 105
  • 1 ≤ nums[i][j] ≤ 106
  • The input represents a jagged 2D array (rows can have different lengths)

Visualization

Tap to expand
Diagonal Traverse II INPUT Jagged 2D Array: nums 1 2 3 row 0 4 5 row 1 6 row 2 Diagonal Groups (i+j) d=0: [1] d=1: [2,4] d=2: [3,5,6] col 0 col 1 col 2 nums = [[1,2,3],[4,5],[6]] Rows have different lengths ALGORITHM (Hash Map) 1 Create HashMap Key = i+j (diagonal index) 2 Traverse Array For each nums[i][j] 3 Group by Diagonal map[i+j].append(value) 4 Collect Results Iterate diagonals in order Hash Map Contents: Key Values (list) 0 [1] 1 [2, 4] 2 [3, 5, 6] Elements added top-to-bottom within each diagonal Time: O(n), Space: O(n) FINAL RESULT Traversal Order: 1 2 3 4 5 6 d=0 d=1 d=2 Collection Order: d=0: 1 d=1: 2 --> 4 d=2: 3 --> 5 --> 6 Output Array: 1 2 4 3 5 6 OK - Correct! [1,2,4,3,5,6] Key Insight: Elements on the same diagonal share the property that (row + column) indices are equal! Using a hash map with key = i+j groups elements by diagonal. Within each diagonal, elements are naturally ordered from top to bottom when iterating rows first, then columns. TutorialsPoint - Diagonal Traverse II | Hash Map Approach
Asked in
Facebook 45 Amazon 38 Google 32 Microsoft 28
89.2K Views
High Frequency
~18 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen