β Spiral rotation(Contest)
Spiral rotation easy Time Limit: 2 sec Memory Limit: 128000 kB
```java
import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
// don't change the name of this class
// you can add inner classes if needed
class Main {
public static void main (String[] args) {
// Your code here
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[][] arr = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = sc.nextInt();
}
}
int left = 0;
int right = m - 1;
int top = 0;
int bottom = n - 1;
while (left <= right && top <= bottom) {
for (int i = left; i <= right; i++) {
System.out.print(arr[top][i] + " ");
}
top++;
for (int i = top; i <= bottom; i++) {
System.out.print(arr[i][right] + " ");
}
right--;
if (top <= bottom) {
for (int i = right; i >= left; i--) {
System.out.print(arr[bottom][i] + " ");
}
bottom--;
}
if (left <= right) {
for (int i = bottom; i >= top; i--) {
System.out.print(arr[i][left] + " ");
}
left++;
}
}
}
}
```Last updated