How do I print a 5×5 two-dimensional array in spiral order?
Is there any formula so that I can print an array of any size in spiral order?
How do I print a 5×5 two-dimensional array in spiral order?
Is there any formula so that I can print an array of any size in spiral order?
This question is related to this one: Matrix arrangement issues in php
The answers presented seem to work but are complicated to understand. A very simple way to solve this is divide and conquer i.e., after reading the edge, remove it and the next read will be much simpler. Check out a complete solution in PHP below:
int N = Integer.parseInt(args[0]);
}
Python code:
Java code if anybody is interested.
Input:
4
1 2 3 4
5 6 7 8
9 1 2 3
4 5 6 7
Output: 1 2 3 4 8 3 7 6 5 4 9 5 6 7 2 1
This program works for any n*n matrix..
Hope it helps
Here is my implementation in Java: