n = 10 # just as an example, n can be anything
sum = 0
for var in range(n):
sum += 1
print(sum)
var=0 | 1 |
---|---|
var=1 | 1 |
var=2 | 1 |
… | |
var=n-1 | 1 |
For the calculation of time complexity of above example…as the value of n is 10 so the var will be calculated for 0,1,2,3,4,5,6,7,8,(n-1) or 9 times ,in that case the time complexity would be 1 for each operation which will leads for 1+1+1+1+1+1+1+1+1+1= 10,but how it could be 1+1+1…1 = 4 ,could you please explain this process?
Course: Algorithms for Coding Interviews in Python - Learn Interactively
Lesson: Example 1: Measuring Time Complexity - Algorithms for Coding Interviews in Python