Complete the code to reverse the array using a simple loop.
for i := 0; i < len(arr)/2; i++ { arr[i], arr[len(arr)-1-[1]] = arr[len(arr)-1-[1]], arr[i] }
The index from the end is calculated as len(arr)-1-i to swap elements correctly.
Complete the code to reverse the array using two pointers.
left, right := 0, len(arr)-1 for left < [1] { arr[left], arr[right] = arr[right], arr[left] left++ right-- }
The loop continues while the left pointer is less than the right pointer to swap elements until they meet.
Fix the error in the recursive array reversal function.
func reverse(arr []int, start, end int) {
if start >= [1] {
return
}
arr[start], arr[end] = arr[end], arr[start]
reverse(arr, start+1, end-1)
}The base case should stop when start is greater than or equal to end to avoid overlapping swaps.
Fill both blanks to create a function that reverses an array in place using a for loop.
func reverseArray(arr []int) {
for i, j := 0, len(arr)-1; i [1] j; i, j = i+1, j[2]1 {
arr[i], arr[j] = arr[j], arr[i]
}
}The loop runs while i is less than j, and j decreases by 1 each iteration.
Fill all three blanks to create a map that stores reversed elements with their indices as keys.
func reversedMap(arr []int) map[int]int {
result := make(map[int]int)
for i, v := range arr {
result[[1]] = arr[[2]]
}
return result
}The map key is the original index i, the value is the element from the reversed position len(arr)-1-i, which is v in the loop.
