Ice Cream Parlor

  • + 0 comments

    C++

    vector<int> icecreamParlor(int m, vector<int> arr){
        vector <int> result;
        int expense_left; 
        for (int i = 0; i < arr.size(); i++){
            expense_left = m - arr[i]; // for every entry, calculate expense left
            auto search = find(arr.begin()+i+1, arr.end(), expense_left); // see if expense left is available
            
            //if expense left is available, return 1-based index
            if (search != arr.end()){
                result.push_back(i+1);
                result.push_back(search-arr.begin()+1); // search-arr.begin() gives 0-based indexing, so add 1
                break;
            }
        }
        return result;    
    }