Hi @rohith,
This is Fatimah Abdullah from Educative. Thank you for reaching out to us!
In response to your question, let’s look at each point one by one.
It seems that you are doing a mistake in calculating how many times i++
should run. This statement only runs n
times, not 2n
. The reason is that the condition says that i < n
, so it can only increase n
times. Increasing 2n
times is not possible. So, the total complexity of the outer loop will be 2n + 2
.
First, when we calculate the complexity of the inner loop by itself, it will be 2m + 2
. Now, as we already know that each statement inside the outer loop runs n
times, so as the inner loop is also inside the outer loop, it would also run n
times. Thus, the total complexity of the inner loop is n x (2m + 2) = 2nm + 2n
.
I hope this clears up your confusion. If you have any further queries, please let us know.
Best Regards,
Fatimah Abdullah | Developer Advocate
Educative