Mark As Completed Discussion

Traversing the Outer Edges

To traverse the outer edge, we have to follow the given sequence:

  1. Move right
  2. Move down
  3. Move left
  4. Move up

Once we have moved entirely right, we can increment rowLower. Similarly, once we have traversed down the right-most column we can decrement colUpper. Therefore, to traverse the entire matrix in spiral order, we can follow the given algorithm.

The entire process is shown in the figure below:

Our Algorithm

Here, we share some pseudo-code to help make sense of the logic involved.

TEXT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment