(1)
Yes.
(2)
This algorithm can rotate 2D-arrays with odd dimensions (1x1, 3x3, 5x5 etc). Rotating counter-clockwise is the same as rotating clockwise three times.
private static char[][] rotateRight(char[][] x) {
char[][] rotated = new char[x.length][x.length];
for (int i = 0; i < x.length; i++) {
for (int j = 0; j < x[i].length; j++) {
rotated[j][(x.length - 1) - i] = x[i][j];
}
}
return rotated;
}
For rotating a 4x4 array you might need a different algorithm, but you can find the right algorithm yourself. First write a unit test for 2x2 rotation and then write code which passes the test. Then write a test for 4x4 rotation, make it pass, and the algorithm should be complete (if you like, you can write one more test for 6x6 just to make sure that it passes).
The trick is that you figure out (on pen and paper) how the coordinates of each element change. Here are the notes which I made when writing the above code.
Coordinates when rotating a 3x3 grid right:
before after
0-0 0-2
0-1 1-2
0-2 2-2
1-0 0-1
1-1 1-1
1-2 2-1
2-0 0-0
2-1 1-0
2-2 2-0
Coordinates when rotating a 5x5 grid right:
before after
0-0 0-4
0-1 1-4
0-2 2-4
0-3 3-4
0-4 4-4
1-0 0-3
1-1 1-3
1-2 2-3
1-3 3-3
1-4 4-3
...
Do you notice a pattern?