#include <iostream>
#include <vector>
#include <unordered_map>

class Solution {

    std::vector<int> result {};

public:

    void twoSum(const std::vector<int>& nums, const int target) {
        
        std::unordered_map<int, int> map {}; 
        
        result = {};      
        
        for (int i = 0; i < nums.size(); i++) {
            
            int diff = target - nums[i];
            
            if (map.count(diff) != 0) {
                result = {map[diff], i};
                
            }
            
            map[nums[i]] = i;
        }
    }

    friend std::ostream& operator<<(std::ostream& out, Solution& sol) {
        
        if (sol.result.size() == 2) {
            out << "["<< sol.result[0] << ", " << sol.result[1] << "]";
        } else {
            out << "[]";
        }
        
        return out;
    }

};


int main() {

    Solution sol;

    std::vector<int> nums {1, 2, 3, 4, 5};
    
    for (int target = 1; target < 12; target++) {
        sol.twoSum(nums, target);
        std::cout << sol << '\n';       
    }
    
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: