Two Sum
Two Sum
Problem Statement
Given an array of integers nums and an integer target, return the indices of the two numbers such that they add up to target.
Video Explanation

Approach
To solve this problem, we can use a hash map to store the numbers and their indices. As we iterate through the list, we check if the complement (target - current number) exists in the hash map.
Steps:
-
Initialize:
- Create an empty
hashmap.
- Create an empty
-
Iterate:
- For each number in
nums, calculate its complement. - Check if the complement exists in the hash map.
- If it exists, return the indices.
- Otherwise, add the current number and its index to the hash map.
- For each number in
-
Return:
- If no solution is found, return an empty list.
Solutions
- C++
- Java
- Python
- JavaScript
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> numMap;
for (int i = 0; i < nums.size(); i++) {
int complement = target - nums[i];
if (numMap.count(complement)) {
return {numMap[complement], i};
}
numMap[nums[i]] = i;
}
return {};
}
};
import java.util.HashMap;
import java.util.Map;
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if (map.containsKey(complement)) {
return new int[] { map.get(complement), i };
}
map.put(nums[i], i);
}
return new int[] {};
}
}
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
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
var twoSum = function(nums, target) {
const map = new Map();
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (map.has(complement)) {
return [map.get(complement), i];
}
map.set(nums[i], i);
}
return [];
};
Track Your Progress
Done with this topic? Mark it as complete to track your progress.
💬 Discuss this page
Have a question or spot something confusing in "Two Sum"? Ask below — it's backed by GitHub Discussions, so maintainers get notified like any other GitHub activity.