Sobes.tech
Middle

999999 in range(1,99999999) What is the time complexity of this expression?

sobes.tech AI

Answer from AI

The expression 999999 in range(1, 99999999) in Python executes in O(1) time.

This is because the range object in Python is implemented as a lazy generator with an arithmetic progression, and the in operator for it does not iterate over all elements but calculates membership using a formula.

For example, to check if the number x is in range(start, stop), Python checks that start <= x < stop and that (x - start) % step == 0. This is done in constant time without iterating over all elements.