-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathhas_pair_with_sum.py
More file actions
27 lines (21 loc) · 845 Bytes
/
has_pair_with_sum.py
File metadata and controls
27 lines (21 loc) · 845 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from typing import List, TypeVar
Number = TypeVar("Number", int, float)
"""
Find if there is a pair of numbers that sum to a target value.
Time Complexity:
Before Refactor: O(n^2) — nested loops checking every pair.
After Refactor: O(n) — O(1) set lookups.
Space Complexity:
Before Refactor: O(1) — no additional data structure.
After Refactor: O(n) — in the worst case we store all numbers in numbers_seen.
Optimal Time Complexity:
O(n) — achievable using a set to track previously seen numbers.
"""
def has_pair_with_sum(numbers: List[Number], target_sum: Number) -> bool:
numbers_seen = set()
for current_num in numbers: # O(n)
required_num = target_sum - current_num
if required_num in numbers_seen:
return True
numbers_seen.add(current_num)
return False