I have been told to use an integer array and insert elements into it, which is relatively simple. The difficult part is having to expand the array and retain its existing elements, doing all this within O(log n). This array will later be sorted and turned into a heap, but that can be ignored, since it doesn't pertain to the problem.
The problem I am having is, one of the methods I have to implement is insert, and it must be done in O(log n). I am not given a capacity for my array. Inserting an element is easy enough, but I need to somehow accommodate for the array reaching its capacity, and still be able to insert the integer into the array without throwing an NPE. The insert method must accommodate for this without violating O(log n).
I have considered using ArrayList, where ArrayList.add is O(1), as long as the given/default length hasn't been met, otherwise it is O(n) in order to reallocate a new ArrayList with double the space, and copy the old elements into it.
Is there an implementation where you can extend an array's length and retain/copy its contents, while satisfying O(log n)?