class Solution { public: vector twoSum(vector& nums, int target) { // When you find, e.g., 2, map (2 -> idx 0) for the event // in which you find the 7. std::vector pair(2); std::unordered_map val_to_idx; for (size_t i = 0; i < nums.size(); ++i) { const int other = target - nums[i]; const auto other_idx = val_to_idx.find(other); if (other_idx != val_to_idx.end()) { pair[0] = i; pair[1] = other_idx->second; break; } val_to_idx[nums[i]] = i; } return pair; } };