I didn't get how its O(n^2)...given in image attached according to me it should be O(n)
array[n];
array[0] = 1;
array[1] = 1;
for i = 2 to i = n:
array[i] = array[i-1] + array[i-2]
return array[n]
I didn't get how its O(n^2)...given in image attached according to me it should be O(n)
array[n];
array[0] = 1;
array[1] = 1;
for i = 2 to i = n:
array[i] = array[i-1] + array[i-2]
return array[n]
The picture you've linked is incorrect. Each iteration of that loop takes time O(1) to execute, and it runs O(n) times, so the cost of that loop is O(n). It looks like the slide accidentally multiplied the O(n) total cost of the loop by the O(n) number of iterations to incorrectly get an O(n2) term.
Your analysis is correct - this code does run in time O(n).