📖 Scenario: You are working on a shopping app. You want to find two items whose prices add up to a gift card value.
🎯 Goal: Build a program that finds two numbers in a list that add up to a target sum using a hash map for fast lookup.
📋 What You'll Learn
Create a vector of integers called nums with the exact values: 2, 7, 11, 15
Create an integer variable called target and set it to 9
Use an unordered_map called hash_map to store numbers and their indices
Use a for loop with variable i to iterate over nums
Inside the loop, check if target - nums[i] exists in hash_map
If found, store the indices in a vector called result
Print the two indices stored in result
💡 Why This Matters
🌍 Real World
Finding two items in a list that sum to a gift card value is common in shopping apps and budgeting tools.
💼 Career
This problem teaches hash map usage and efficient searching, which are key skills for software developers working on performance-critical applications.
Progress0 / 4 steps
1
Create the list of numbers
Create a vector of integers called nums with these exact values: 2, 7, 11, 15
DSA C++
Hint
Use std::vector<int> and initialize it with the values inside curly braces.
2
Set the target sum
Create an integer variable called target and set it to 9
DSA C++
Hint
Use int target = 9; to create the variable.
3
Find two indices using hash map
Create an unordered_map called hash_map to store numbers and their indices. Use a for loop with variable i to iterate over nums. Inside the loop, check if target - nums[i] exists in hash_map. If found, create a vector called result with the two indices and break the loop. Otherwise, add nums[i] and i to hash_map.
DSA C++
Hint
Use hash_map.find() to check existence. Store indices in result vector.
4
Print the result indices
Print the two indices stored in the vector result separated by a space.
DSA C++
Hint
Use std::cout to print result[0] and result[1] separated by a space.
Practice
(1/5)
1. What is the main purpose of using a hash map in the classic Two Sum problem solution?
easy
A. To store numbers and their indices for quick lookup
B. To sort the array before searching
C. To store the sum of all pairs
D. To reverse the array elements
Solution
Step 1: Understand the role of hash map
The hash map stores each number as a key and its index as the value for quick access.
Step 2: Explain why quick lookup is needed
Quick lookup helps find if the complement (target - current number) exists without scanning the entire array.
Final Answer:
To store numbers and their indices for quick lookup -> Option A
Quick Check:
Hash map = quick index lookup [OK]
Hint: Hash map stores values for instant complement search [OK]
Common Mistakes:
Thinking hash map sorts the array
Confusing hash map with storing sums
Assuming hash map reverses elements
2. Which of the following C++ code snippets correctly declares the hash map used in the Two Sum problem to store integer keys and values?
easy
A. std::map<int> map;
B. std::unordered_map map;
C. std::unordered_map<int> map;
D. std::vector map;
Solution
Step 1: Identify correct hash map syntax
std::unordered_map declares a hash map with int keys and int values.
Step 2: Explain why others are incorrect
std::map<int> lacks value type; std::unordered_map<int> lacks value type; std::vector is invalid syntax.