Two Sum Problem Statement in C++ and JavaScript Interview
![]() |
| Two Sum Problem in C++ and JavaScript |
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.
Examples
Example 1:
Input: nums = [2, 7, 11, 15], target = 9
Output: [0, 1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
Example 2:
Input: nums = [3, 2, 4], target = 6
Output: [1, 2]
Example 3:
Input: nums = [3, 3], target = 6
Output: [0, 1]
Constraints
- 2 ≤ nums.length ≤ 10⁴
- -10⁹ ≤ nums[i] ≤ 10⁹
- -10⁹ ≤ target ≤ 10⁹
- Only one valid answer exists.
Follow-up
Can you come up with an algorithm that is less than O(n²) time complexity?
Two Sum Problem in C++
#include <vector>
#include <unordered_map>
using namespace std;
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> m;
vector<int> result;
for (int i = 0; i < nums.size(); i++) {
if (m.find(nums[i]) == m.end()) {
m[target - nums[i]] = i;
} else {
result.push_back(m[nums[i]]);
result.push_back(i);
break;
}
}
return result;
}
};
🔍 Line-by-Line Explanation (C++)
unordered_map<int, int> m;
A hash map to store key = target - nums[i] and value = index i.
Loop through the array nums
i is the index of the current number.
Check if nums[i] exists in map
If not, it stores the complement (target - nums[i]) as the key and i as the value.
If yes, it means we previously stored the number that, when added to nums[i], gives the target.
Return indices
m[nums[i]] → index of the complement
i → current index
Two Sum Problem in JavaScript
function twoSum(nums, target) {
const sum = {};
for (let i = 0; i < nums.length; i++) {
if (!sum.hasOwnProperty(target - nums[i])) {
sum[nums[i]] = i;
} else {
return [i, sum[target - nums[i]]];
}
}
}
const result = twoSum([2, 7, 11, 15], 9);
🔍 Line-by-Line Explanation (JavaScript)
const sum = {};
Object used as a hash map to store values and their indices.
Loop through
nums
For each number, it checks if its complement (target - nums[i]) exists in the sum object.
If the complement does not exist
Store the current value and its index:
sum[nums[i]] = i;
If it does exist
Return the pair of indices:
[i, sum[target - nums[i]]]
❓ FAQs: Two Sum Problem
Q1. What is the time complexity of these solutions?
A: Both C++ and JavaScript solutions run in O(n) time, where n is the number of elements in the array. This is because they iterate through the array once using a hash map (unordered_map/object) for constant-time lookups.
Q2. Why do we use a hash map / object?
A: To store the complement (target - nums[i]) of each number for quick lookup. This avoids nested loops and improves performance.
Q3. Why can’t we use the same element twice?
A: Because the problem strictly says so. Hence, we ensure the current element is not the one we’re trying to pair it with.
Q4. What happens if there’s no solution?
A: In these implementations, it is assumed that there is exactly one solution. If not, these codes may return an empty array or undefined behavior. For safety, you could add a condition to handle such cases.
Q5. Can this approach work for multiple pairs?
A: No, this code only returns the first valid pair that meets the condition. To find all such pairs, you’d need to modify the logic.
Q6. Which one is better—C++ or JavaScript?
A: Both are efficient. Use the one that fits your platform or project. C++ is often used in system-level programming and competitive coding, while JavaScript is common in web development.

