Two Sum Problem Classic Hash Solution
📖 Scenario: You are helping a friend who runs a small store. They want to find two items that together cost exactly a certain amount of money. You will write a program to find the positions of these two items in the price list.
🎯 Goal: Build a program that finds two numbers in a list that add up to a target value using a hash map (dictionary) for fast lookup.
📋 What You'll Learn
Create a list called
prices with the exact values: [2, 7, 11, 15]Create a variable called
target and set it to 9Use a dictionary called
seen to store numbers and their indices while looping through pricesFind two indices
i and j such that prices[i] + prices[j] == targetPrint the list of the two indices as the final output
💡 Why This Matters
🌍 Real World
Finding two items in a list that add up to a specific value is useful in shopping apps, budgeting tools, and financial software.
💼 Career
This problem is a common coding interview question and helps develop skills in using hash maps for efficient searching.
Progress0 / 4 steps