Array Insertion at Middle Index in DSA Go - Time & Space Complexity
We want to understand how the time needed to insert an element in the middle of an array changes as the array grows.
Specifically, how does the work increase when the array gets bigger?
Analyze the time complexity of the following code snippet.
package main
import "fmt"
func insertMiddle(arr []int, val int) []int {
mid := len(arr) / 2
arr = append(arr, 0) // increase slice size by 1
copy(arr[mid+1:], arr[mid:]) // shift elements right
arr[mid] = val // insert value
return arr
}
func main() {
arr := []int{1, 2, 3, 4, 5}
arr = insertMiddle(arr, 99)
fmt.Println(arr)
}
This code inserts a value in the middle of an integer array by shifting elements to the right.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Shifting elements to the right using
copywhich moves elements from the middle to the end. - How many times: The number of elements shifted is about half the array size (n/2) in the worst case.
As the array size grows, the number of elements to shift grows roughly in proportion to the size.
| Input Size (n) | Approx. Operations (shifts) |
|---|---|
| 10 | About 5 |
| 100 | About 50 |
| 1000 | About 500 |
Pattern observation: The work grows linearly with the size of the array.
Time Complexity: O(n)
This means the time to insert in the middle grows directly with the array size.
[X] Wrong: "Inserting in the middle is always fast because we just put the value there."
[OK] Correct: We must move all elements after the middle to make space, which takes time proportional to how many elements are shifted.
Understanding this helps you explain why arrays are not always the best choice for frequent middle insertions, showing your grasp of data structure trade-offs.
"What if we changed the array to a linked list? How would the time complexity of inserting in the middle change?"
Practice
Solution
Step 1: Calculate middle index for odd length array
For an array of length 7, middle index = length / 2 = 7 / 2 = 3 (integer division).Step 2: Confirm insertion position
The new element should be inserted at index 3 to be in the middle.Final Answer:
3 -> Option AQuick Check:
Middle index = 7/2 = 3 [OK]
- Using length/2 + 1 for middle index
- Confusing zero-based index with count
- Using length/2 as float instead of integer
Solution
Step 1: Understand slice insertion in Go
To insert at middle, split slice at middle index, append new element slice, then append remaining slice.Step 2: Check syntax correctness
arr = append(arr[:len(arr)/2], append([]int{x}, arr[len(arr)/2:]...)...) correctly uses append with slices and variadic syntax to insert element at middle.Final Answer:
arr = append(arr[:len(arr)/2], append([]int{x}, arr[len(arr)/2:]...)...) -> Option BQuick Check:
Correct slice append syntax = arr = append(arr[:len(arr)/2], append([]int{x}, arr[len(arr)/2:]...)...) [OK]
- Trying to append element directly without wrapping in slice
- Incorrect index calculation with +1
- Appending element at end instead of middle
arr := []int{1, 2, 3, 4}
mid := len(arr) / 2
arr = append(arr[:mid], append([]int{99}, arr[mid:]...)...)
fmt.Println(arr)Solution
Step 1: Calculate middle index
len(arr) = 4, so mid = 4 / 2 = 2.Step 2: Insert 99 at index 2
arr[:2] = [1, 2], arr[2:] = [3, 4]. Insert 99 between them.Step 3: Resulting array
New arr = [1, 2, 99, 3, 4]Final Answer:
[1 2 99 3 4] -> Option AQuick Check:
Insert 99 at index 2 = [1 2 99 3 4] [OK]
- Inserting element at wrong index
- Appending element at end instead of middle
- Misunderstanding slice append syntax
nums := []int{10, 20, 30, 40}
mid := len(nums) / 2
nums = append(nums[:mid], 50, nums[mid:])
fmt.Println(nums)Solution
Step 1: Check append syntax
append(nums[:mid], 50, nums[mid:]...) tries to append int and slice variadically, which is invalid.Step 2: Correct approach
Wrap 50 in a slice: append(nums[:mid], append([]int{50}, nums[mid:]...)...).Final Answer:
Cannot append element and slice together without wrapping element in slice -> Option CQuick Check:
Append element + slice needs nested append [OK]
- Appending element and slice directly together
- Incorrect middle index calculation
- Forgetting variadic ... operator
Solution
Step 1: Calculate middle index
len(data) = 5, so mid = 5 / 2 = 2 (integer division).Step 2: Insert 100 at index 2 correctly
Use nested append: append(data[:mid], append([]int{100}, data[mid:]...)...).Step 3: Print updated slice
Prints [5 10 100 15 20 25].Final Answer:
mid := len(data)/2 data = append(data[:mid], append([]int{100}, data[mid:]...)...) fmt.Println(data) -> Option DQuick Check:
Insert at middle with nested append = mid := len(data)/2 data = append(data[:mid], append([]int{100}, data[mid:]...)...) fmt.Println(data) [OK]
- Using mid+1 instead of mid for insertion
- Appending element at end instead of middle
- Appending element and slice without wrapping element
Ready to prove it?
Pass the test and this pattern moves unknown
