I am trying to find out the complexity of the given code below as a function of the problem size n.
sum = 0;
if (EVEN(n))
for (i = 0; i < n; i++)
if (i%2 == 0)
O(logn)
else
sum ++;
else
sum = sum + n;
Here is my answer but I am not sure if I did it correct.
sum=0; is equal to O(1).
The first if else loop consists of a nested for loop, with a nested if statement. So it would be n times the code inside the for loop. Since its if statement and will be either block 1 or block 2. Since O(logn) is slower than sum ++ which is O(1) it is O(logn). So it is O(n)*O(logn).
Hence the final answer would be O(1) + n*O(logn). Is this correct??