We did an exercise in class today dealing with big-O notation. Here is one of the problems:
void modifyArray(int a[], int size)
{
int max = a[0];
for (int i = 1; i < size / 2; ++i)
{
if (max < a[i])
max = a[i];
}
for (int j = 1; j <= size * size; ++j)
{
++max;
cout << max;
}
}
My intuition tells me that f(n) = n/2 + n2 = O(n2) but according to my professor the answer is simply O(n). Could anyone explain to me why and when we just change what we consider to be the input size?
I understand that it is not a nested loop -- that is not what is confusing me. I don't understand why for a given input size
, the second loop is only considered to be O(n). The only way I can make sense of this is if we isolate the second loop and then redefine the input size to simply being n = size^2. Am I on the right track?