Recursive 2D Array Row Reversal
What is printed when the following program is executed?
public class ReverseRow {
public static void reverseRow(int[] row, int start, int end) {
if (start >= end) return;
int temp = row[start];
row[start] = row[end];
row[end] = temp;
reverseRow(row, start + 1, end - 1);
}
public static void reverseAll(int[][] arr, int i) {
if (i == arr.length) return;
reverseRow(arr[i], 0, arr[i].length - 1);
reverseAll(arr, i + 1);
}
public static void main(String[] args) {
int[][] grid = { {1, 2, 3}, {4, 5, 6} };
reverseAll(grid, 0);
for (int i = 0; i < grid.length; i++) {
for (int n : grid[i]) {
System.out.print(n + " ");
}
System.out.println();
}
}
}
A
1 2 3
4 5 6
B
1 2 3
6 5 4
C
3 2 1
4 5 6
D
3 2 1
6 5 4
Question Leaderboard
| Rank | |||||
|---|---|---|---|---|---|
| #1 | kaisuki | 1 | 1 | 0m 00s | 100 |
| #2 | midnightasdark | 1 | 4 | 0m 43s | 27 |
| #3 | y.seong2027 | 1 | 2 | 6m 24s | -294 |
Items per page:
10
1 – 3 of 3
APFIVE