Hash Tables: Ice Cream Parlor

  • + 2 comments
    void solve(vector <int> arr, int money) {
        // Complete this function
        int firstpick = 0;
        int secondpick;
        unordered_map<int,int> costTofavor;
        for(int i=0;i<arr.size();i++){
            secondpick = i;
            if(costTofavor.count(money-arr[i])){
                firstpick = costTofavor[money-arr[i]];
                cout<<firstpick+1<<" "<<secondpick+1<<endl;
                return;
            }
            costTofavor[arr[i]]=i;
         }
    }
    

    My C++ approch to this question. Inspired by leetcode one-pass hash table solution.