ArrayList Insertion Time Complexity
Determine the overall time complexity when inserting n elements into the middle of an ArrayList using add(index, element).
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(n^2)
B
O(1)
C
O(n)
D
O(n log n)
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 |
APFIVE