Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Hide Buttons

Print All Elements of Two Dimensional Array in Spiral

Objective: This question was asked in Amazon interview for the Software development Engineer position, Write an algorithm to print all the elements of two dimensional array in spiral.
Example :

Print 2D array in Spiral

Print 2D array in Spiral

Input: Two dimensional array

Output: All array elements printed in spiral.

Approach:

  • Start printing from first row.
  • Print row and columns, forward and backward alternatively
  • With every iteration of (either row or column), reduce the size of an row or column by 1
  • Call recursively

Complete Code:

Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

You may also like...

  • Hari Kiran

    Awesome tutorials. Please make more. I am a huge fan of your tutorials .

    • Sumit Jain

      Thanks Hari, Your comment is motivation for me 🙂

  • Nishant Dehariya

    really awesome i find everything easy and better than geeksforgeeks although i m c++ guy.

%d bloggers like this: