How can it be O(N^2) ??
I can't understand it
Please paste the question here.
int sum = 0;
void calcSum(vector v, int i)
{
if(i == v.size())
return;
sum += v[i];
calcSum(v, i+1);
}
How can it’s time complexity be O(N^2) ?
Assuming vector is defined as vector< int > v ,its time complexity will be O(n^2).
Because total n calls will be there and on every call vector gets copied to new vector which is O(n) for each call . Therefore O(n^2) for n calls.
If the vector was passed by reference using & operator then the complexity would be O(n) because there no copying would take place.
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.