How to find the time complexity of the following code?

Could you explain how to find the time complexity of the following code? Any help was appreciated.

int boo(n) {
    if (n > 0)
    {
         return 1 + boo(n/2) + boo(n/2);
    }
    else 
    {
         return 0;
    }
}
+4
source share
1 answer

enter image description here

Sometimes it's good to write down. When you start, it sums up 1 + boo (n / 2) + boo (n / 2), which is on the second line.

And each of these n / 2 also starts

etc .. and others.

So in the end, while the number of calls grows exponentially, the number of repetitions is only logarithmic, which at the end deletes each other and you get O (N).

PS: , ( ), ( , )

+1

Source: https://habr.com/ru/post/1662295/


All Articles