Complete the code to reverse the array using a simple loop.
for (int i = 0; i < arr.length / 2; i++) { int temp = arr[i]; arr[i] = arr[arr.length - 1 - [1]]; arr[arr.length - 1 - i] = temp; }
The index to swap with is the mirror position from the end, which is arr.length - 1 - i.
Complete the code to reverse the array using two pointers.
int left = 0, right = arr.length - 1; while (left < [1]) { int temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; left++; right--; }
left <= right causing unnecessary swaps.The loop continues while left is less than right, swapping elements from both ends.
Fix the error in the recursive array reversal method by completing the base case condition.
void reverse(int[] arr, int start, int end) {
if (start [1] end) {
return;
}
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
reverse(arr, start + 1, end - 1);
}start <= end which causes infinite recursion.The base case stops recursion when start is greater than or equal to end, meaning the array is fully reversed.
Fill both blanks to create a reversed copy of the array using a for loop.
int[] reversed = new int[arr.length]; for (int i = 0; i < arr.length; i++) { reversed[i] = arr[[1] - i - [2]]; }
i instead of arr.length in the calculation.The reversed index is calculated by subtracting the current index and 1 from the array length.
Fill all three blanks to reverse the array in place using a for loop with swapping.
for (int i = 0; i < [1] / 2; i++) { int temp = arr[i]; arr[i] = arr[[2] - i - [3]]; arr[[2] - i - [3]] = temp; }
arr.length directly in the loop condition without dividing by 2.The loop runs till half the array length. The swap index is calculated as arr.length - i - 1.
