Array Insertion at Beginning in DSA C++ - Time & Space Complexity
We want to understand how long it takes to add a new item at the start of an array.
How does the time needed change as the array gets bigger?
Analyze the time complexity of the following code snippet.
void insertAtBeginning(int arr[], int &size, int capacity, int value) {
if (size == capacity) return; // no space
for (int i = size - 1; i >= 0; i--) {
arr[i + 1] = arr[i];
}
arr[0] = value;
size++;
}
This code inserts a new value at the start of the array by shifting all existing elements one position to the right.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that shifts elements to the right.
- How many times: It runs once for each existing element, so
sizetimes.
As the array grows, the number of shifts grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 shifts |
| 100 | 100 shifts |
| 1000 | 1000 shifts |
Pattern observation: The work grows directly with the number of elements; doubling the array size doubles the work.
Time Complexity: O(n)
This means the time to insert at the beginning grows linearly with the array size.
[X] Wrong: "Inserting at the beginning is always fast because it's just one step."
[OK] Correct: Actually, all existing elements must move one step right, so the time depends on how many elements are there.
Knowing this helps you explain why arrays are not always best for front insertions and when linked lists might be better.
"What if we used a linked list instead of an array? How would the time complexity change?"
Practice
Solution
Step 1: Understand array structure
An array stores elements in continuous memory locations.Step 2: Effect of insertion at beginning
To insert at the start, all elements must move right to free the first position.Final Answer:
All existing elements shift one position to the right to make space. -> Option AQuick Check:
Insertion at start shifts elements right = B [OK]
- Thinking new element replaces first without shifting
- Assuming array size decreases
- Believing array sorts automatically
Solution
Step 1: Shift elements right from end
Loop from last index `n` down to 1, moving each element to the right by one.Step 2: Insert new element at index 0 and increase size
Place `x` at `arr[0]` and increment `n`.Final Answer:
for(int i = n; i > 0; i--) arr[i] = arr[i-1]; arr[0] = x; n++; -> Option CQuick Check:
Shift right from end, insert at 0 = C [OK]
- Shifting from start to end overwrites data
- Not incrementing array size
- Inserting without shifting elements
int arr[5] = {2, 4, 6, 8, 10};
int n = 5;
int x = 1;
for(int i = n; i > 0; i--) {
arr[i] = arr[i-1];
}
arr[0] = x;
n++;
for(int i = 0; i < n; i++) {
std::cout << arr[i] << " ";
}Solution
Step 1: Shift elements right to make space
Elements move right: arr[5]=arr[4]=10, arr[4]=arr[3]=8, arr[3]=arr[2]=6, arr[2]=arr[1]=4, arr[1]=arr[0]=2.Step 2: Insert 1 at arr[0] and print
arr[0]=1, array becomes [1,2,4,6,8,10], printed with space.Final Answer:
1 2 4 6 8 10 -> Option BQuick Check:
Shift right + insert 1 at start = A [OK]
- Not shifting elements correctly
- Printing old array size
- Inserting at wrong index
for(int i = 0; i < n; i++) {
arr[i+1] = arr[i];
}
arr[0] = x;
n++;Solution
Step 1: Analyze loop direction
Loop moves from 0 to n-1, shifting elements forward, overwriting original data.Step 2: Correct loop direction
Loop must run backward from n to 1 to shift elements safely without overwrite.Final Answer:
Loop should start from `n` and go down to 1 to avoid overwrite. -> Option DQuick Check:
Backward loop prevents overwrite = D [OK]
- Shifting elements forward overwrites data
- Forgetting to increment array size
- Inserting element at wrong position
Solution
Step 1: Check if `x` is smaller than first element
If yes, shift all elements right and insert `x` at index 0 to keep order.Step 2: If `x` is not smallest, find correct position
Find proper index to insert `x` to maintain sorted order, not necessarily at start.Final Answer:
If `x` is smaller than `arr[0]`, shift elements right and insert `x` at start; else find correct position. -> Option AQuick Check:
Insert at start only if smaller, else find position = A [OK]
- Always inserting at start breaks sorting
- Sorting entire array after insertion wastes time
- Replacing last element loses data
Ready to prove it?
Pass the test and this pattern moves unknown
