-
Notifications
You must be signed in to change notification settings - Fork 12.5k
/
Copy pathtwo_num.py
58 lines (45 loc) · 1.71 KB
/
two_num.py
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""
Author: Anurag Kumar (mailto:[email protected])
Description:
This function finds two numbers in a given list that add up to a specified target.
It returns the indices of those two numbers.
Constraints:
- Each input will have exactly one solution.
- The same element cannot be used twice.
Example:
>>> two_sum([2, 7, 11, 15], 9)
[0, 1]
"""
from typing import List, Optional
def two_sum(nums: List[int], target: int) -> Optional[List[int]]:
"""
Finds indices of two numbers in 'nums' that add up to 'target'.
Args:
nums (List[int]): List of integers.
target (int): Target sum.
Returns:
Optional[List[int]]: Indices of the two numbers that add up to the target,
or None if no such pair is found.
"""
if len(nums) < 2:
raise ValueError("Input list must contain at least two numbers.")
if not all(isinstance(num, int) for num in nums):
raise TypeError("All elements in the list must be integers.")
# Dictionary to track seen values and their indices
seen_values = {}
for index, value in enumerate(nums):
complement = target - value
if complement in seen_values:
return [seen_values[complement], index]
seen_values[value] = index
return None
# Example usage
if __name__ == "__main__":
example_nums = [2, 7, 11, 15]
example_target = 9
result = two_sum(example_nums, example_target)
if result:
num1, num2 = example_nums[result[0]], example_nums[result[1]]
print(f"Indices that add up to {example_target}: {result} (Values: {num1} + {num2})")
else:
print(f"No combination found that adds up to {example_target}.")