Loading…
Loading…
Charting your lesson…
Opening your lesson…
In-Place Array Rearrangement
Lesson 7 of 8
6 steps•about 11 min•11 flashcards
Step 1/ 61 min
"Do it in O(1) extra space" is a phrase that appears in half of all array interviews. In-place rearrangement — moving zeros, rotating, partitioning by a pivot, Dutch-flag sorting — shows you can manipulate an array using swaps and pointers rather than a helper array.
Core idea: rearrange elements by swapping within the array itself, guided by pointers, so no proportional extra memory is used. The recurring tools are the two-pointer write index and the reversal trick.
Step 2/ 61 min
"Move all zeros to the end, keep order of non-zeros." A slow write pointer marks the next slot for a keeper; a fast read pointer scans:
write = 0
for read in 0..n-1:
if a[read] != 0:
swap(a[write], a[read]); write += 1
O(n) time, O(1) space, and stable for the non-zeros.
Rotate an array right by k. The elegant O(1)-space method reverses in three steps:
rotate([1,2,3,4,5], k=2):
reverse all -> [5,4,3,2,1]
reverse first 2 -> [4,5,3,2,1]
reverse rest -> [4,5,1,2,3]
Three linear passes → O(n) time, O(1) space. Always take k = k % n first.
Sorting an array of 0s, 1s, 2s (or partitioning around a pivot) in one pass uses three pointers: low, mid, high.
low = mid = 0; high = n-1
while mid <= high:
if a[mid] == 0: swap(a[low], a[mid]); low++; mid++
elif a[mid] == 1: mid++
else: swap(a[mid], a[high]); high-- # do NOT advance mid here
Each element is examined O(1) times → O(n) time, O(1) space. This is also QuickSort's partition core.
Step 3/ 61 min
Sort [2, 0, 2, 1, 1, 0] of 0/1/2 in place. Run Dutch-flag: after the pass the array becomes [0, 0, 1, 1, 2, 2]. Note when we swap a 2 to the back we do not advance mid, because the swapped-in value is unexamined — forgetting that is the classic bug.
Step 4/ 61 min
Whenever the constraint says O(1) extra space or "modify the array in place": move/segregate elements, rotate, reverse, partition, remove duplicates from a sorted array, or cyclic-sort problems ("place value v at index v to find the missing number").
Step 5/ 61 min
mid — the new arrival is unchecked.k mod n or you loop needlessly (and reverse-of-reverse cancels out).In the Dutch-flag partition, advancing mid after swapping with the high pointer. The value swapped in from the back has not been inspected yet, so you may leave a 2 stranded in the middle. Advance mid only after handling 0s and 1s.
Step 6/ 6· Worked examples1 min
Problem
Move all zeros to the end of [0, 1, 0, 3, 12] while keeping the order of non-zero elements, using O(1) extra space.
Try it on paper first.
Recall
Keep this
"Do it in place, O(1) extra space" is a classic interview constraint. In-place rearrangement problems test whether you can move elements around with swaps and pointers instead of allocating a second array — a real memory-efficiency skill.
Common in-place tools:
| Problem | Technique | Time | Space |
|---|---|---|---|
| Rotate array by k | reversal trick | O(n) | O(1) |
| Sort 0s,1s,2s | Dutch flag | O(n) | O(1) |
| Find missing/duplicate | cyclic sort | O(n) | O(1) |
| Move zeroes / partition | two pointers | O(n) | O(1) |
| Next permutation | scan + swap + reverse | O(n) | O(1) |
Rotate, sort 0/1/2, move zeroes, next permutation, or find the missing/duplicate number under an explicit O(1)-space constraint. Interviewers reward swaps-and-pointers over an auxiliary array.
Rearrange with swaps and pointers, not extra memory: reversal trick rotates in O(n)/O(1), Dutch flag sorts 0/1/2 in one pass, cyclic sort places [1..n] values, and sign-marking flags visits. Always O(n) time, O(1) space.
You've read 0 of 6 steps
Now lock it in — questions straight after reading are what make it stick.