Is an array declared like this:
int array[M]
, O(1)
in space or O(n)
? where M is some fixed value. To me O(n)
makes sense because it is not just a single variable but an entire array. But then i think it could be O(1)
since we have a fixed size and it is not changing!
If your array is of a fixed size and it does not vary with the size of the input it is O(1)
since it can be expressed as c * O(1)
= O(1)
, with c
being some constant. An example would be if you needed an array of size 5 to hold state in your algorithm that runs over a million (or some other arbitrary number) integers. The important thing is M
and N
are independent.
If however M
represents the size of your input or a value that is directly dependent of the input size (i.e. N/2
or some other linear function), then really M
grows along with your N
, the input size so it would be O(N)
. An example would be an array that holds all input numbers of which you want to run an algorithm over (i.e determining the sum of the squares).