I'm reading one cool book, and there was a task to determine the time complexity of the code in the worst case. The code performs integer division (a > 0, b > 0):
int div(int a, int b) {
int count = 0;
int sum = b;
while (sum <= a) {
sum += b;
count++;
}
return count;
}
My answer was O(n), since, in the worst case, when we have a > 0, b = 1, we will iterate through a times in the while() loop.
The answer in the book was O(a/b) which makes sense. But, if we're considering the worst case, then
O(a/1) => O(a) <=> O(n) (letter in parentheses doesn't really matter in this case).
So, the answer O(n) is correct as well. Isn't it?