Depending on how you increased the storage volume, and k is small enough, it can be O (1) for all cases or something else.
By βhow,β I mean, in managed languages, you can create a new array of size n + k, and then copy the old array (size n) to a new one and finally replace the link from the old array to the new one. It will be: O (1) (array creation, this assumption) + O (n) (data copy) + O (1) (help). We ignore the execution of the garbage collector because it is implementation dependent. In unmanaged languages, you can use something similar to realloc so that old elements are saved without having to be copied, because the new storage occupies the same memory area, only expanded to the number of required elements. In this case, it is O (1) for all cases. Please note that, however, sometimes expanding the storage to the number of required items is not possible due to memory fragmentation, so an approach similar to managed languages ββis used instead (but is performed implicitly using the realloc implementation). For this, the complexity is the same as in managed languages: O (n).
What is the answer from a practical point of view, I hope you find the answer from an analytical point of view using the answer above. Good luck.
source share