int foo(int n) { int p = 1 ; if (n && !(n&(n-1))) // When n = 1 or n = 0 then condition is true otherwise false return n; while (p<n) { p <<= 1;//Its equivalent to p * 2^i where i = 1 to n-1 } return p; }
Therefore in worst case
$^{2^{k-1}}$ < n
Taking log on both sides
$k-1 = \log_{2} n$
Therefore T(n) = O($\log_{2} n$)
64.3k questions
77.9k answers
244k comments
80.0k users