What is the O-notation for the following code?
cin >> N;
for (int count = 1; count <= N; count++)
{
for (int count2 = 1; count2 <= count; count2++)
}
a) O (log N)
b) O (N)
c) O (N^2)
d) O (N^3)
outer loop is O(N)
and the inner loop is also O(N) so you multiply these together..
i'll get N*N = N^2 so the answer is "c" O(N^2)
but what if
cin >> N;
for (int count = 1; count <= N; count++)
{
for (int count2 = 1; count2 <= count; count2++)
{
cout << count1 + count2;
}
}
count1 = N
count2 = N
count1 + count2 = N + N??
so what is the answer in this case??...O(N) or O(N^2)???
a) O (log N)
b) O (N)
c) O (N^2)
d) O (N^3)