Space time complexity

while(n–){ int a[100] }
how space complexity in this code is O(1), i mean why

Hello @sktg99,

The time complexity:
In terms of processing time:
it’s not O(1).
The time complexity is O(n) as the loop is iterating for n times where n is the input variable.

O(1) is consider as constant time.

In terms of access time:
O(1) means the time to access something is independent of the number of items in the collection.

O(N) would mean the time to access an item is a proportional to the number (N) of items in the collection.

Hope, this would help.

I asked space complexity
how that is constant in that code

Hey @sktg99,

Space complexity is a measure of the amount of working storage an algorithm needs. That means how much memory, in the worst case, is needed at any point in the algorithm.
While computing space complexity we consider the Auxilary/extra space taken by the program.
Auxiliary space is temporary or extra space used by an algorithm. This temporary space allocated in order to solve the problem.

O(1) Space Complexity:
It means the amount of memory the program consumes doesn’t depend on the input. Your algorithm should use the same amount of memory for all inputs i.e. program takes constant time for the execution independent of N.

This is the case for the following example:

while(n--){
int a[100];
}

Reason:

  1. The extra memory allocated is independent of n and is a constant of 100*4(bytes).
    i.e. for each value of n, the same amount of memory would be assigned to the program.

  2. As explained by Sir, the array is allocated once only. So, it won’t be n*100.

Hope, it is clear now.
Give a like if you are satisfied.