Array Insertion at Middle Index in DSA Typescript - Time & Space Complexity
When we insert an element in the middle of an array, the time it takes depends on how many elements need to move.
We want to know how the work grows as the array gets bigger.
Analyze the time complexity of the following code snippet.
function insertMiddle(arr: number[], value: number): number[] {
const mid = Math.floor(arr.length / 2);
for (let i = arr.length; i > mid; i--) {
arr[i] = arr[i - 1];
}
arr[mid] = value;
return arr;
}
This code inserts a value into the middle of an array by shifting elements to the right.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that shifts elements one position to the right.
- How many times: About half of the array elements are moved, so roughly n/2 times.
As the array grows, the number of elements to move grows roughly in half.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 5 moves |
| 100 | About 50 moves |
| 1000 | About 500 moves |
Pattern observation: The work grows linearly with the size of the array.
Time Complexity: O(n)
This means the time to insert grows directly with the number of elements in the array.
[X] Wrong: "Inserting in the middle is always fast because we just add one element."
[OK] Correct: Because elements after the middle must move to make space, the work grows with array size.
Understanding this helps you explain why some data structures are better for insertions than arrays, showing your grasp of efficiency.
"What if we inserted at the end of the array instead of the middle? How would the time complexity change?"
Practice
Solution
Step 1: Understand array indexing
Arrays have positions starting from 0 to length-1. The middle index is usually length/2 (rounded down).Step 2: Define insertion at middle
Inserting at middle means placing the new element at this middle index, shifting elements after it to the right.Final Answer:
Adding the element exactly in the center position of the array -> Option AQuick Check:
Middle insertion = center position [OK]
- Confusing insertion with replacement
- Inserting at start or end instead of middle
- Not shifting elements after insertion
arr?Solution
Step 1: Understand splice method
splice(index, 0, value) inserts value at index without removing elements.Step 2: Check other options
push adds at end, unshift adds at start, direct assignment replaces element.Final Answer:
arr.splice(Math.floor(arr.length / 2), 0, 99); -> Option CQuick Check:
splice with 0 delete count inserts [OK]
- Using push or unshift instead of splice
- Replacing element instead of inserting
- Incorrect index calculation
const arr = [10, 20, 30, 40]; arr.splice(Math.floor(arr.length / 2), 0, 25); console.log(arr);
Solution
Step 1: Calculate middle index
arr.length = 4, middle index = Math.floor(4/2) = 2.Step 2: Insert 25 at index 2
splice(2, 0, 25) inserts 25 before element at index 2 (which is 30), shifting elements right.Final Answer:
[10, 20, 25, 30, 40] -> Option BQuick Check:
Inserted 25 at index 2 [OK]
- Inserting at wrong index
- Replacing element instead of inserting
- Misunderstanding splice parameters
const nums = [1, 2, 3, 4, 5]; const mid = nums.length / 2; nums.splice(mid, 0, 50); console.log(nums);
Solution
Step 1: Check mid calculation
nums.length = 5, mid = 5 / 2 = 2.5 (a float).Step 2: Understand splice behavior
splice coerces 2.5 to integer 2 (ToInteger(2.5) = 2). Inserts 50 at index 2, shifting 3,4,5 right: [1, 2, 50, 3, 4, 5].Final Answer:
[1, 2, 50, 3, 4, 5] -> Option DQuick Check:
Float index coerced to 2 [OK]
- Confusing with unshift (insert at start)
- Confusing with push (append at end)
- Inserting at index 3 instead of 2
arr = [5, 10, 15, 20, 25], write a TypeScript function insertMiddle that inserts a given value exactly in the middle of the array. What will be the array after calling insertMiddle(arr, 12)?Solution
Step 1: Calculate middle index for odd length
arr.length = 5, middle index = Math.floor(5/2) = 2.Step 2: Insert 12 at index 2
Using splice(2, 0, 12) inserts 12 before element at index 2 (which is 15), shifting elements right.Final Answer:
[5, 10, 12, 15, 20, 25] -> Option AQuick Check:
Inserted 12 at middle index 2 [OK]
- Inserting at wrong index for odd length
- Appending instead of inserting
- Replacing element instead of inserting
Ready to prove it?
Pass the test and this pattern moves unknown
