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 :
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
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
Awesome tutorials. Please make more. I am a huge fan of your tutorials .
Thanks Hari, Your comment is motivation for me 🙂
really awesome i find everything easy and better than geeksforgeeks although i m c++ guy.