Prefix-Suffix Swap

We are given an array of letters, arr, and a length, n, which is a multiple of 3. The goal is to modify arr in place to move the prefix of length n/3 to the end and the suffix of length n/3 to the beginning.

Example:

  • Input: arr = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I']
  • Output: ['G', 'H', 'I', 'D', 'E', 'F', 'A', 'B', 'C']
1def prefixSuffixSwap(arr: List[str]) -> None:
2 n = len(arr)
3 k = n // 3
4 l, r = 0, n - k
5
6 while l < k:
7 arr[l], arr[r] = arr[r], arr[l]
8 l += 1
9 r += 1
l
A
0
B
1
C
2
D
3
E
4
F
5
r
G
6
H
7
I
8
Step 1 / 4
Step 1:
Initialize n=9, k=3, l=0, r=6.
Pointers: l=0, r=6
Focus: select @ [0, 6]