Arrays: indexing, traversal, insertion and deletion costs
Arrays give instant access by index, but inserting or deleting in the middle shifts every later element. Watch elements shift one by one.
Arrays: access, insert and delete
The idea
On a packed shelf, a new book in the middle means sliding every later book along.
An array is the same: values sit in a row with no gaps. Reading any slot is quick, but making room costs moves.
Intuition
Cells sit side by side in memory. Index 3 is one jump from the start: 100 + 3 × 4.
The computer finds a[i] with one multiply and one add: start address + i × cell size. That is why reading or writing a[i] is O(1) for any i.
To put 5 at index 1, the values 8, 1, 9 and 6 must each move one cell right first.
Complexity
Inserting at index i moves n − i values: all n at the front, none at the end.
On average half the values move, so insert and delete are O(n). Extra space is O(1): the loop needs only k.
Finding a value, not an index, still checks cells one by one. Search is O(n) unless the array is sorted.
Pitfalls
Shift from the right end. Copying 3 8 1 9 6 left to right writes 8 over every later value.
a[2] ← a[1] puts 8 in index 2, then a[3] ← a[2] copies that same 8 again. Starting at the end moves each value before it is overwritten.
A fixed array needs a free slot to insert. When it is full, writing a[n] goes past its end.
Variations
If order does not matter, delete by moving the last value into the gap. That takes O(1).
A linked list inserts without shifting, but reaching index i means walking i links.
Reverse and rotate in place
The idea
To play a playlist backwards, swap the first and last songs, then the next pair inward.
No second list is needed. Each swap puts two songs in their final places, so the work stops halfway.
Intuition
Two pointers walk toward each other. Every pair outside them is already reversed.
Rotating right by 3 moves the last 3 values to the front. Three reversals do it in place.
Reversing the whole array puts 7 6 5 at the front, but backwards. Reversing each part again restores their order.
Complexity
Rotating by one cell k times costs n × k shifts. Three reversals cost about n swaps for any k.
Each reversal of m values does m / 2 swaps. The three reversals touch n + k + (n − k) values, so 2n / 2 = n swaps. Time O(n), extra space O(1).
Copying into a second array also takes O(n) time, but it needs n extra cells.
Pitfalls
Take k mod n first. Rotating 7 values by 10 is the same as rotating by 3.
Without the mod, rev(0, k − 1) reads past the end when k > n. With n = 0, skip the rotation to avoid dividing by zero.
Stop when the pointers meet. Swapping until i reaches the end reverses everything twice.
Variations
Rotating left by k is rotating right by n − k. Reverse the first k, then the rest, then the whole array.
Code
Pseudocode
insert(a, n, i, x) // a has room for n + 1 values
for k ← n down to i + 1
a[k] ← a[k - 1]
a[i] ← x
return n + 1
Python
def insert(a, n, i, x):
for k in range(n, i, -1):
a[k] = a[k - 1]
a[i] = x
return n + 1
JavaScript
function insert(a, n, i, x) {
for (let k = n; k > i; k--) {
a[k] = a[k - 1];
}
a[i] = x;
return n + 1;
}
Java
static int insert(int[] a, int n, int i, int x) {
for (int k = n; k > i; k--) {
a[k] = a[k - 1];
}
a[i] = x;
return n + 1;
}
Cost at a glance
| Access | O(1) |
|---|---|
| Search | O(n) |
| Insert | O(n) |
| Delete | O(n) |
| Space | O(n) |
Practice problems (7)
Insert at index Warm-up
The array [3, 8, 1, 9, 6] has one free slot at the end. Insert 7 at index 2 and answer each question before the step plays.
Count shifts Warm-up
Delete the value at index 1 from [6, 2, 9, 4, 7, 1, 8]. Count how many values shift before each step plays.
Reverse in place Warm-up
Reverse the array nums in place and return it. Do not create a second array: swap values with two pointers that walk toward each other.
Rotate by k Standard
Rotate nums to the right by k steps and return it. After one step the last value moves to the front: [1, 2, 3] becomes [3, 1, 2].
k can be larger than the length. Aim for O(n) time and O(1) extra space.
Delete at index by shifting Standard
Delete the value at index i from nums and return the array without it. Do it the way a fixed array must: move every later value one cell left, then drop the last cell.
Do not use built-in helpers such as splice, pop(i) or del.
Match array operations to their cost Standard
Match each operation on an array of n values to its worst-case time.
Move zeroes to the end in place Challenge
Move every 0 in nums to the end while keeping the other values in their original order. Change the array in place and return it.
Shifting values left for each zero is O(n²). Find an O(n) way with O(1) extra space.
Related topics
Next on the path: Dynamic arrays: capacity, growth, amortized append