Array Insertion at Middle Index in DSA Java - 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.
public void insertMiddle(int[] arr, int n, int value) {
int mid = n / 2;
for (int i = n - 1; i >= mid; i--) {
arr[i + 1] = arr[i];
}
arr[mid] = value;
}
This code inserts a new value in 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 by one.
- How many times: About half of the array elements are moved.
As the array size grows, the number of elements to shift grows roughly half as fast.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 5 shifts |
| 100 | 50 shifts |
| 1000 | 500 shifts |
Pattern observation: The work grows linearly with the size of the array.
Time Complexity: O(n)
This means the time to insert grows in direct proportion to the array size.
[X] Wrong: "Inserting in the middle is always fast because we just put the value there."
[OK] Correct: We must move many elements to make space, which takes time proportional to the number of elements shifted.
Understanding how array insertions scale helps you explain trade-offs between arrays and other data structures like linked lists.
"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 insertion behavior
In Java, arrays have fixed size, so to insert an element in the middle, elements from that index onward must shift right to make space.Step 2: Effect of insertion at middle index
The element at the middle index and all elements after it move one position to the right, making room for the new element.Final Answer:
Elements after the middle index shift right to make space for the new element. -> Option CQuick Check:
Insertion shifts elements right [OK]
- Thinking the new element replaces existing one without shifting
- Assuming array size grows automatically
- Believing insertion adds element at the end always
Solution
Step 1: Identify correct shifting direction
To insert at middle, elements from middle to end must shift right, so loop runs backward from end to middle.Step 2: Verify code correctness
int mid = n / 2; for (int i = n - 1; i >= mid; i--) arr[i + 1] = arr[i]; arr[mid] = x; correctly shifts elements right starting from the last index to middle, then inserts x at mid.Final Answer:
int mid = n / 2; for (int i = n - 1; i >= mid; i--) arr[i + 1] = arr[i]; arr[mid] = x; -> Option AQuick Check:
Backward loop shifts right, then insert [OK]
- Shifting elements left instead of right
- Looping forward causing overwrite
- Assigning new element before shifting
int[] arr = {1, 2, 3, 4, 5, 0};
int n = 5;
int x = 99;
int mid = n / 2;
for (int i = n - 1; i >= mid; i--) {
arr[i + 1] = arr[i];
}
arr[mid] = x;
for (int i = 0; i <= n; i++) {
System.out.print(arr[i] + " ");
}Solution
Step 1: Calculate middle index and shift elements
n=5, mid=2. Loop shifts elements from index 4 to 2 right by one: arr[5]=arr[4]=5, arr[4]=arr[3]=4, arr[3]=arr[2]=3.Step 2: Insert x at middle and print array
arr[2]=99. Final array: [1, 2, 99, 3, 4, 5]. Printing elements from 0 to 5 outputs: 1 2 99 3 4 5 0Final Answer:
1 2 99 3 4 5 0 -> Option DQuick Check:
Shift right + insert at mid = 1 2 99 3 4 5 0 [OK]
- Forgetting to shift elements before insertion
- Printing extra zero at end
- Incorrect middle index calculation
int mid = arr.length / 2;
for (int i = mid; i < arr.length; i++) {
arr[i + 1] = arr[i];
}
arr[mid] = x;Solution
Step 1: Analyze loop direction and effect
The loop runs forward from mid to end, assigning arr[i+1] = arr[i], which overwrites elements before they are copied.Step 2: Correct approach for shifting elements
To avoid overwrite, elements must be shifted right starting from the end backward to mid.Final Answer:
Loop runs forward causing overwriting of elements during shift. -> Option BQuick Check:
Shift must go backward to avoid overwrite [OK]
- Shifting elements forward causing data loss
- Misunderstanding loop boundaries
- Inserting element before shifting
Solution
Step 1: Calculate middle index and prepare for insertion
Find middle index as n/2. Since array has extra space, shifting elements right from end to middle creates space.Step 2: Shift elements right and insert new element
Shift elements starting from last valid index backward to middle index to avoid overwriting. Insert x at middle index.Step 3: Print the updated array
Print elements from index 0 to n (inclusive) to show the array with inserted element.Final Answer:
Calculate middle index; shift elements right from end to middle; insert x at middle; print array from 0 to n. -> Option AQuick Check:
Shift right backward, insert at mid, print full array [OK]
- Inserting before shifting elements
- Shifting elements forward causing overwrite
- Printing wrong array range
Ready to prove it?
Pass the test and this pattern moves unknown
