Question-8 quiz doubt STL

Q8. Vector STL#8

A student just learned about vectors. Now he wants to implement a class vector which can support dynamic array of integers.

He writes the following code :

class myVector
{
int * data; // pointer to underlying array to store elements
int capacity; //size of the underlying array
int elementsInVec; //number of elements currently in the vector
public:
myVector(){
data = new int[1000];
elementsInVec = 0, capacity = 1000;
}
void push_back(int x)
{
if( elementsInVec == capacity)
{
int * temp = new int[capacity + 1];
for(int i = 0; i < capacity; i++)
temp[i] = data[i];
temp[capacity++] = x;
delete data;
data = temp;
}
else data[elementsInVec] = x;
elementsInVec++;
}

    // more code
}

What is the time complexity of push_back as implemented by the student? Let N be the current size of the vector.

I don’t get it. How is it O(1) for 1st 1000 calls and O(N) afterwards?

hello @govilayush
intially we have array of size 1000.
so to insert at some index we take O(1) .

now for size >1000
for each insertion this code will run, which is equivalent to O(capacity) . ||| o(n)