Field manual
In-place array transform
O(n)Rewrite the array inside its own buffer instead of allocating a new one: you overwrite earlier slots as you read forward, and the final length is smaller or equal to the start.
Signals
modify the array in placeO(1) extra spacedo not allocate another array/stringreverse/rotate/partition/move elements over a single bufferreturn the new length after removing duplicates
Template
function inPlaceTransform(arr) {
let write = 0;
for (let read = 0; read < arr.length; read++) {
if (shouldKeep(arr[read])) {
arr[write] = arr[read];
write++;
}
}
return write; // new logical length
}Looks similar, isn't
- Two pointers (same direction): In-place is the GOAL here (O(1) space, rewrite one buffer); a same-direction slow/fast pair is one MECHANIC that reaches that goal. Reversing a fixed-size block or rotating an array is also in-place but uses no slow/fast pair at all.
n up to 1e5..1e6 -> O(n): one pass over the buffer, no second array, so the space stays O(1).
Learn this pattern