What is the time complexity of inserting at the end in dynamic arrays?
A. O(1)
B. O(n)
C. O(logn)
D. Either O(1) or O(n)
Answer:D
What is the time complexity of inserting at the end in dynamic arrays?
A. O(1) B. O(n) C. O(logn) D. Either O(1) or O(n)
Explanation: Depending on whether the array is full or not, the complexity in a dynamic array varies. If you try to insert into an array that is not full, then the element is simply stored at the end, this takes O(1) time. If you try to insert into an array that is full, first you will have to allocate an array with double the size of the current array and then copy all the elements into it and finally insert the new element, this takes O(n) time.