ArrayList Insertion Time Complexity
What is the overall time complexity of the following code segment, which inserts n elements into the middle of an ArrayList?
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
int n = 100; // assume n elements
for (int i = 0; i < n; i++) {
int mid = list.size() / 2;
list.add(mid, i);
}
}
}
A
O(1)
B
O(n log n)
C
O(n)
D
O(n^2)
Question Leaderboard
| Rank | |||||
|---|---|---|---|---|---|
| #1 | kaisuki | 1 | 3 | 0m 00s | 80 |
| #2 | hiboyshello | 0 | 2 | 0m 00s | -20 |
| #3 | ponneban000 | 0 | 3 | 4m 04s | -274 |
| #4 | y.seong2027 | 1 | 3 | 9m 40s | -500 |
Items per page:
10
1 – 4 of 4
APFIVE