William Gwede
last year
Williamcountry asked

Does quadratic time complexity only apply to nested loops?

Kelish Rai
Expert
last year
Kelish Rai answered

That's right. Quadratic time complexity (O(n²)) is commonly associated with nested loops, where the number of operations grows in proportion to the square of the input size.

For example, consider this Python code:

# This function runs n * n times
def print_pairs(arr):
    for i in range(len(arr)):
        for j in range(len(arr)):  
            print(i, j)

lst = [1, 2, 3, 4, 5]

print_pairs()

Here, for every element in the list, another loop runs through all elements again, leading to O(n²) complexity.

Python
This question was asked as part of the Complexity Calculation course.