How to traverse through array values in a spiral matrix

Michael Tong
Aug 23, 2020 · 2 min read
Photo by Joshua Sortino on Unsplash

Imagine you are walking through a dungeon and you want to find the destination point in the middle.

That’s right…printing values in a spiral matrix feels like that.

To give you some context, let’s say we have the following double array arr:

As you can see, the numbers spirals 1 to 25 from the outer layers, left to right, top to bottom, right to left, and bottom to up as it dives across other inner layers in a similar passion.

The question is, how do we traverse the whole array in a spiral manner and print them in the right format?

Here is my suggested solution:

  • Step 1: have a row begin and row-end count, column begin, and column end count. These counts will be used to track boundaries and conditions to end looping through the matrix.
  • Step 2: add items going from top row, left to right. Afterward, increment row begin count.
  • Step 3: add items on the rightmost column, from top to bottom. Afterward, decrement column end count.
  • Step 4: Check if row begin is less than or equal to row end count. This is to prevent adding/printing existing elements that have already been traversed. If the condition matches, then we traverse on the current column end count(on the first iteration, this will be iterating on the last row, going from right column to the leftmost column) until it hits columnBegin count.
  • Step 5:check if the column begins is less than or equal to columned count. This is to prevent adding/printing existing elements that have already been traversed. If the condition matches, then we traverse on the current row end count(on the first iteration, this will be iterating on left-most column, going from bottom to up on the same column) until it hits rowBegin count.

Here is the implementation for the algorithm:

That’s it! You just learned how to solve the spiral matrix problem.

Sign up for 💌 Weekly Newsletter

By Weekly Webtips

Get the latest news on the world of web technologies with a series of tutorial Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Michael Tong

Written by

Coding is nothing but a renovation of ideas through fundamental concepts. Stay sharp and stay hungry to learn!

Weekly Webtips

Explore the world of web technologies through a series of tutorials

Michael Tong

Written by

Coding is nothing but a renovation of ideas through fundamental concepts. Stay sharp and stay hungry to learn!

Weekly Webtips

Explore the world of web technologies through a series of tutorials

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store