Say theres a recursive function which runs n times and a nested for loop that runs n^2 times, what will be its time complexity O(n) or O(n^3)
For example :
fun(int n) {
if(n==1) return;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
printf("A");
}
Say theres a recursive function which runs n times and a nested for loop that runs n^2 times, what will be its time complexity O(n) or O(n^3)
For example :
fun(int n) {
if(n==1) return;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
printf("A");
}
the time complexity of the above function is O(n^3).
look as it as for every n, the loops s will run n(n+1)/2 times and this will take place n time. So the total number of iterations will be (n^2)(n+1)/2 which is O(n^3).