Spiral rotation(Contest)
Spiral rotation easy Time Limit: 2 sec Memory Limit: 128000 kB
Last updated
Spiral rotation easy Time Limit: 2 sec Memory Limit: 128000 kB
Last updated
Problem Statement :
There has been an attack on SHIELD. Nick Fury has given you the responsibility of protecting all the information but during the chaos he forgot to tell you how to login into the classified information. Just then a βsecret codeβ appears on the screen.
Print the information in a 2D integer array of size (N x M) in a spiral form. That is, you need to print in the order followed for every iteration: a. First row(left to right) b. Last column(top to bottom) c. Last row(right to left) d. First column(bottom to top) Mind that every element will be printed only once. Input The first line of input contains two integers N and M, the next N lines of input contains M space- separated integers each depicting the values of the matrix.
Constraints:- 2 <= N, M <= 500 1 <= Matrix[][] <= 1000000 Output Print the matrix in spiral form as shown in the example. Example Sample Input:- 3 3 1 2 3 4 5 6 7 8 9
Sample Output:- 1 2 3 6 9 8 7 4 5
Sample Input:- 4 5 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40
Sample Output:- 2 4 6 8 10 20 30 40 38 36 34 32 22 12 14 16 18 28 26 24
link: