
Introduction
Puzzles are good exercise for the brain. They help in enhancing the cognitive abilities of the brain helping with Problem Solving and related skills. There are numerous types of puzzles; each one having a logic inherent to itself which helps in cracking it. A good puzzle well is actually like a good mystery that we may have read about or watched on TV. It has the finest of hints which help in reaching its solution.
The following article discusses one such puzzle so let's get right to it.
Problem Statement
A spiral matrix problem always takes a 2-Dimensional array of, say, n-rows and m-columns as input to the program and then prints all the array elements in reverse of the spiral order.
Example :
If this is the input matrix -
The Spiral Matrix would be formed like this -
Thus the output of the program, which is the reverse spiral form, will be as follows-
16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
Recommended: Try the Problem yourself before moving on to the solution.
Explanation
This spiral order begins at the very top left corner of the input matrix and then moves on to the element it encounters while forming a loop around the input matrix, towards the center of this particular matrix, and always in a clockwise manner. Finally, the reverse of this order is given as output to the problem statement of printing an array in the reverse spiral order.