Introduction
Imagine you want to know how long a task will take as the size of the input grows. Time complexity helps us understand how the time needed changes when we handle bigger problems.
Imagine sorting a deck of cards. If you have just a few cards, sorting is quick. But if you have hundreds, some ways of sorting take much longer than others. Time complexity is like knowing how your sorting time grows as you add more cards.
┌───────────────┐
│ Input Size n │
└──────┬────────┘
│
▼
┌─────────────────────────┐
│ Time Complexity Growth │
│ │
│ O(1) ──┐ │
│ O(log n)│ │
│ O(n) │ │
│ O(n²) │ │
└────────┴───────────────┘def linear_search(arr, target): for i in range(len(arr)): if arr[i] == target: return i return -1 print(linear_search([1, 3, 5, 7, 9], 7))
O(n) time complexity mean in simple terms?O(n)O(n) means the time grows in direct proportion to the input size n.O(1) or logarithmic O(log n), O(n) means if input doubles, time roughly doubles.O(1) represents constant time complexity.for i in range(n):
for j in range(n):
print(i, j)n times.n times.O(n^2).def example(n):
for i in range(n):
for j in range(10):
print(i, j)n times.O(n).n items. It first sorts the list using an efficient algorithm, then scans it once to find duplicates.O(n log n) time.O(n).O(n log n).Pass the test and this pattern moves unknown