Model answer
The address of a[i] is base+i×sizeof(T). That fixed arithmetic takes constant time and does not depend on the number of elements, so indexed access is O(1).
Use the key terms, then explain the reasoning.
Arrays store same-type elements in contiguous memory, enabling constant-time indexing and cache-friendly traversal.
An array maps index i to an address using base + i × element-size. This arithmetic explains O(1) random access and why all elements must have a fixed-size representation.
std::vector adds a size and capacity around a dynamic array. When capacity is exhausted it allocates a larger block and moves elements, making an individual growth operation O(n) but a sequence of appends O(1) amortised per append.
Insert 25 at index 2
| Operation | Best | Average | Worst | Extra space |
|---|---|---|---|---|
| Index access | O(1) | O(1) | O(1) | O(1) |
| Search | O(n) | O(n) | O(n) | O(1) |
| Append to vector | O(1) | O(1) amortized | O(n) | O(1) |
| Middle insert/delete | O(n) | O(n) | O(n) | O(1) |
std::vector provides contiguous dynamic storage. Insertion in the middle shifts the suffix.
Open a prompt when you are ready to check your answer.
End-of-topic practice
The idea is fresh—use it now. Run visible cases, inspect failures, and then submit against hidden tests.
Rotate a vector right by k positions without allocating another vector.
Find the maximum sum of a non-empty contiguous subarray, including when every value is negative.