Why is the time complexity O(n2) and not O(n)

Predict the time complexity of the following recursive function, given the vector is of size N and the initial call is calSum(v, 0).

int sum = 0;

void calcSum(vector v, int i)

{

if(i == v.size())
    return;
sum += v[i];
calcSum(v, i+1);

}

hi @pradyumn25jain since the vector is being passed by value, it will be copied to a new vector in each call. Copying the vector will take O(n) time, doing so n times will make the overall time complexity of the program as O(n*n)

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.