title: How to perform spiral order traversalin one dimensional array?
body: <blockquote>
    <p><strong>Possible Duplicates:</strong><br>
    <a href="https://stackoverflow.com/questions/398299/looping-in-a-spiral">Looping in a spiral</a><br>
    <a href="https://stackoverflow.com/questions/7183735/writing-a-string-in-a-spiral">Writing a string in a spiral</a><br>
    <a href="https://stackoverflow.com/questions/726756/print-two-dimensional-array-in-spiral-order">Print two-dimensional array in spiral order</a><br>
    <a href="https://stackoverflow.com/questions/945265/2d-array-in-spiral-order">2d Array in Spiral Order</a></p>
  </blockquote>
  
  
  
  <p>We have a data in the form of matrix</p>
  
  <pre><code>0 0 0 0 0
  0 1 2 3 0
  0 4 5 6 0
  0 7 8 9 0
  0 0 0 0 0
  </code></pre>
  
  <p>which is stored in a 1d array in this fashion</p>
  
  <pre><code>[0 0 0 0 0 0 1 2 3 0 0 4 5 6 0 0 7 8 9 0 0 0 0 0 0]
  </code></pre>
  
  <p>This is a zero padded 3x3 array transformed into 5x5. We know the start index and end index.</p>
  
  <p>As we see, we can perform 25 operations and print all values, but instead if we go in spiral order we should ideally do this in only 9 operations.</p>
  
  <p>Does anyone know how to do this? </p>
  
  <p>We know the number of rows and number of columns. Here it would be rows=5 cols=5.</p>
  
  <p>Hence start index would be rows+1 and end index would be rows*cols-6</p>
  
  <p>I'm visualizing it as a spiral order traversal.</p>