| preferred AP College board partner for AP classes
AP Computer Science A/Unit 7: ArrayList
Start Practice TestPractice Test
About Exam
medium Solved by 5 students
ArrayList Insertion Time Complexity
< Prev
Next >

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)

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer
Report Question

Question Leaderboard

Rank
User
Correct Count
Attempt Count
Time
Score
#1kaisuki13 0m 00s 80
#2hiboyshello02 0m 00s -20
#3ponneban00003 4m 04s -274
#4y.seong202713 9m 40s -500
Items per page:
10
1 – 4 of 4
No comments yet. Be the first to comment!

AI Tutor

How can I help?

APFIVE © 2020.
Email: apfive@apfive.org|Privacy Policy