I am not looking for the answer here but just how to find the worst/best case of the following problem (in theta notation); for loops are generally (theta(n)), which would make the best and worst case that but I think something else is going on here. Any help would be appreciated.
Input: x (an integer), n (an integer)
addOnes(x, n) {
if x > n then
for i <- 1 to n
return x + n
else
for i <- x to n
x <- x + n
return x
Edit Answer:
Because of return x + n the constant (theta(1)) would be the best.
Best = (theta(1))
Worst = (theta(n))
There are two branches, and both are reachable. To find best and worst case complexity overall, find the best and worst case complexity of each loop, then the best case complexity overall is the better of the best case complexities for the two branches. Similar for the worst case complexity. And there’s a small gotcha built into the problem for the naive to trip over, so be alert.