I know how to find the time complexity for almost any option (simple function, function with loops, etc.), but I can't figure out how to determine time complexity of function that is calling another function, specifically if the calling function is inside a loop.
I wrote some functions, that I use as example.
int g(int k) {
int i=0;
while(k>0) {
i += k;
k= k/2;
}
return i;
}
int f(int n) {
int m = 0;
for (int i=0; i<n; ++i) {
for (int j=i; j<n; ++j) {
m += g(j);
}
}
return m;
}
I can't figure out: do I have to consider the time complexity of function g()
, and if I have to how to calculate it in function f()
? Or do I just ignore function g()
and include just the function calls in function f()
?