Complete the code to initialize the starting row index for spiral traversal.
int top = [1];The top row index starts at 0 because matrix indexing begins at 0.
Complete the code to update the left column index after traversing the top row.
left = [1] + 1;
After traversing the top row, we move the left boundary right by increasing left by 1.
Fix the error in the loop condition to traverse the bottom row from right to left.
for (int i = right; i >= [1]; i--) { result.add(matrix[bottom][i]); }
The bottom row is traversed from right to left, so the loop runs while i >= left.
Fill both blanks to correctly update the boundaries after traversing the right column.
for (int i = top; i <= bottom; i++) { result.add(matrix[i][right]); } right = [1] - 1; top = [2] + 1;
After traversing the right column, we move the right boundary left by 1 and move the top boundary down by 1.
Fill all three blanks to complete the while loop condition and the final boundary update after traversing the left column.
while (top <= [1] && left <= [2]) { // traversal code for (int i = bottom; i >= top; i--) { result.add(matrix[i][left]); } left = [3] + 1; }
The loop continues while top is less or equal to bottom and left is less or equal to right. After traversing the left column, left boundary moves right by 1.
