educative.io

Concern about result

Could someone explain why it is O(n)? I think it is O(nlogn) because the outside loop run logn time and nested loop run n time, so complexity is O(nlogn)

Hi @Hoang
the value of the variable var is changing exponentially in the code which is being used by both inner and outer loop. If you will see the how many times the increment operation is being performed on variable sum, that is 15 where the value of n is 10 and log(n) will be 4 with ceiling. So in short it was executed n + log(n) and if we will take highest value among these then it will be O(n).

Please let me know if there is still any confusion. Thanks.