Walk two indices toward each other from the ends of the array, swapping at each step. Stops when the indices meet or cross. Demonstrates the two-pointer pattern with the smallest possible state.

Algorithm

Canonical input [1, 2, 3, 4, 5, 6, 7] (odd length, middle element stays put) yields three swap frames and reverses to [7, 6, 5, 4, 3, 2, 1].

two pointers `left` starts at index `0`, `right` starts at `n - 1`. Each loop iteration swaps `arr[left]` and `arr[right]` and moves the pointers toward each other.

Basic Implementation

basic.go
Replay: real traced execution (multi-file project)
package main

import "fmt"

func main() {
	arr := []int{1, 2, 3, 4, 5, 6, 7}
	left := 0
	right := len(arr) - 1
	for left < right {
		arr[left], arr[right] = arr[right], arr[left]
		left = left + 1
		right = right - 1
	}
	fmt.Println(arr)
}
  1. arr ← [1, 2, 3, 4, 5, 6, 7]

    5func main() {6	arr := []int{1, 2, 3, 4, 5, 6, 7}7	left := 0
    values this step[1, 2, 3, 4, 5, 6, 7]arr
  2. left ← 0

    6arr := []int{1, 2, 3, 4, 5, 6, 7}7left := 08right := len(arr) - 1
    values this step0left[1, 2, 3, 4, 5, 6, 7]arr
  3. right ← 6

    7left := 08right := len(arr) - 19for left < right {
    values this step6right0left
  4. arr ← [7, 2, 3, 4, 5, 6, 1]

    9for left < right {10	arr[left], arr[right] = arr[right], arr[left]11	left = left + 1
    values this step[1, 2, 3, 4, 5, 6, 7] [7, 2, 3, 4, 5, 6, 1]arr0left6right
  5. left ← 1

    10arr[left], arr[right] = arr[right], arr[left]11left = left + 112right = right - 1
    values this step0 1left
  6. right ← 5

    11	left = left + 112	right = right - 113}
    values this step6 5right
  7. arr ← [7, 6, 3, 4, 5, 2, 1]

    9for left < right {10	arr[left], arr[right] = arr[right], arr[left]11	left = left + 1
    values this step[7, 2, 3, 4, 5, 6, 1] [7, 6, 3, 4, 5, 2, 1]arr1left5right
  8. left ← 2

    10arr[left], arr[right] = arr[right], arr[left]11left = left + 112right = right - 1
    values this step1 2left
  9. right ← 4

    11	left = left + 112	right = right - 113}
    values this step5 4right
  10. arr ← [7, 6, 5, 4, 3, 2, 1]

    9for left < right {10	arr[left], arr[right] = arr[right], arr[left]11	left = left + 1
    values this step[7, 6, 3, 4, 5, 2, 1] [7, 6, 5, 4, 3, 2, 1]arr2left4right
  11. left ← 3

    10arr[left], arr[right] = arr[right], arr[left]11left = left + 112right = right - 1
    values this step2 3left
  12. right ← 3

    11	left = left + 112	right = right - 113}
    values this step4 3right
  13. for left < right

    8right := len(arr) - 19for left < right {10	arr[left], arr[right] = arr[right], arr[left]
    values this step[7, 6, 5, 4, 3, 2, 1]arr3left3right

Complexity

  • Time: O(n)
  • Space: O(1)

Implementation notes

  • Go: idiomatic tuple swap arr[left], arr[right] = arr[right], arr[left] keeps the swap step visible without leaning on a helper that hides the lesson; the standard library has no in-place reverse for []int.
  • left := 0 and right := len(arr) - 1 are plain int indices; the left < right guard handles the meet-in-the-middle exit honestly for the odd-length canonical input.
  • The replay shows both left and right, the values about to be swapped, and the array contents after the swap. The loop-exit frame is the moment the pointers meet.