2D Array Spiral Traversal
Consider the following method:
public static int[][] spiral(int n) {
int[][] result = new int[n][n];
int value = 1;
int top = 0, bottom = n - 1, left = 0, right = n - 1;
while (top <= bottom && left <= right) {
for (int c = left; c <= right; c++) {
result[top][c] = value++;
}
top++;
for (int r = top; r <= bottom; r++) {
result[r][right] = value++;
}
right--;
if (top <= bottom) {
for (int c = right; c >= left; c--) {
result[bottom][c] = value++;
}
bottom--;
}
if (left <= right) {
for (int r = bottom; r >= top; r--) {
result[r][left] = value++;
}
left++;
}
}
return result;
}
What value is stored at index [1][1] in the 2D array returned by the method call spiral(4)?
A
11
B
6
C
10
D
13
Question Leaderboard
Not enough data yet to show leaderboard.
