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!
Is an array declared like this: int array[M] , O(1) in space or O(n)
Share
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 asc * O(1)=O(1), withcbeing 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 isMandNare independent.If however
Mrepresents the size of your input or a value that is directly dependent of the input size (i.e.N/2or some other linear function), then reallyMgrows along with yourN, the input size so it would beO(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).