Two Sum
Problem Statement
Write a function solve that finds two numbers in a list that add up to a target value. Return a list with their indices in order. If the target cannot be made, return an empty list.
Example:
Input: [2, 7, 11, 15], target=9
Output: [0, 1]
Visualization
Number Map:
{}
Python Solution
def two_sum(nums, target): num_map = {} for i, num in enumerate(nums): complement = target - num if complement in num_map: return [num_map[complement], i] num_map[num] = i return []# Example usagenums = []target = 9result = two_sum(nums, target)print(f"Input: nums = {nums}, target = {target}")print(f"Output: {result}")