Question 1 : O(n)
because it increments by constant (1).
first loop O(n)
second loop also O(n)
total O(n) + O(n) = O(n)
Question 2 : O(lg n)
it’s binary search.
it’s O(lg n)
, because problem halves every time.
if the array is size n
at first second is n/2
then n/4
….. 1
.
n/2^i = 1
=> n = 2^i
=> i = log(n)
.