Array Insertion at Middle Index in DSA C# - Time & Space Complexity
We want to understand how long it takes to insert an element in the middle of an array.
How does the time needed change as the array gets bigger?
Analyze the time complexity of the following code snippet.
int[] InsertAtMiddle(int[] arr, int value) {
int n = arr.Length;
int mid = n / 2;
int[] newArr = new int[n + 1];
for (int i = 0; i < mid; i++) {
newArr[i] = arr[i];
}
newArr[mid] = value;
for (int i = mid; i < n; i++) {
newArr[i + 1] = arr[i];
}
return newArr;
}
This code inserts a new value in the middle of an array by creating a new array and shifting elements.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Two for-loops copying elements before and after the middle index.
- How many times: Each loop runs about n/2 times, so together they run roughly n times.
As the array size grows, the number of copy operations grows roughly the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 copy steps |
| 100 | About 100 copy steps |
| 1000 | About 1000 copy steps |
Pattern observation: The work grows directly with the size of the array.
Time Complexity: O(n)
This means the time to insert grows in a straight line with the array size.
[X] Wrong: "Inserting in the middle is as fast as adding at the end."
[OK] Correct: Inserting in the middle requires shifting many elements, unlike adding at the end which can be faster.
Understanding this helps you explain why arrays are not always best for insertions, a key skill in coding interviews.
"What if we used a linked list instead of an array? How would the time complexity change?"
Practice
Solution
Step 1: Understand array insertion behavior
When inserting at the middle, elements from that index onward must move right to avoid overwriting.Step 2: Effect on array size and elements
The array size increases by one, and elements after the middle index shift right to make space.Final Answer:
Elements after the middle index shift right to make space for the new element. -> Option BQuick Check:
Insertion shifts elements right = A [OK]
- Thinking the array size decreases
- Replacing element without shifting
- Assuming no change in elements
Solution
Step 1: Identify List insertion method
List<T> in C# uses Insert(index, value) to insert at any position.Step 2: Calculate middle index and insert
Middle index is numbers.Count / 2; so Insert(numbers.Count / 2, 10) inserts 10 at middle.Final Answer:
numbers.Insert(numbers.Count / 2, 10); -> Option DQuick Check:
Insert(index, value) syntax = B [OK]
- Using non-existent methods like AddAtMiddle
- Confusing Add and Insert parameters
- Wrong method names
List<int> nums = new List<int> {1, 2, 3, 4, 5};
nums.Insert(nums.Count / 2, 99);
Console.WriteLine(string.Join(", ", nums));Solution
Step 1: Calculate middle index
nums.Count is 5, so middle index = 5 / 2 = 2.Step 2: Insert 99 at index 2
Original list: [1, 2, 3, 4, 5]. After insertion: [1, 2, 99, 3, 4, 5].Final Answer:
1, 2, 99, 3, 4, 5 -> Option CQuick Check:
Insert at index 2 shifts elements right = D [OK]
- Inserting at wrong index
- Appending instead of inserting
- Miscounting middle index
int[] arr = {1, 2, 3, 4};
int middle = arr.Length / 2;
arr[middle] = 99;
Solution
Step 1: Understand array size and insertion
Arrays in C# have fixed size; you cannot insert elements to increase size.Step 2: Fix by using List<int> and Insert method
Use List<int> which supports dynamic insertion with Insert(index, value).Final Answer:
Error: Cannot insert in fixed-size array; use List<int> and Insert method. -> Option AQuick Check:
Fixed-size arrays can't insert = A [OK]
- Thinking assignment inserts element
- Misunderstanding array size limits
- Wrong index calculation assumption
Solution
Step 1: Understand array limitations
Arrays have fixed size; direct insertion is not possible without creating a new array.Step 2: Use List for dynamic insertion
List<int> allows insertion at any index easily; after insertion, convert back to array if needed.Final Answer:
Use a List<int>, insert at middle index, then convert back to array if needed. -> Option AQuick Check:
List supports insertion; arrays do not [OK]
- Trying to assign directly in fixed array
- Using Array.Resize incorrectly
- Manually copying arrays without List
Ready to prove it?
Pass the test and this pattern moves unknown
