Ice Cream Parlor

Sort by

recency

|

27 Discussions

|

  • + 0 comments

    Typescript

    function icecreamParlor(m: number, arr: number[]): number[] {
      let obj = {} as any;
    
      for (let i = 0; i < arr.length; i++) {
        obj[arr[i]] = i;
      }
    
      for (let j = 0; j < arr.length; j++) {
        const target = m - arr[j];
        if (obj[target] && obj[target] !== j) {
          return [j + 1, obj[target] + 1];
        }
      }
    
      return [];
    }
    
  • + 0 comments

    Java

    public static List<Integer> icecreamParlor(int m, List<Integer> arr) {
            Map<Integer, Integer> flavorMap = new HashMap<>();
            
            for (int i = 0; i < arr.size(); i++) {
                int currentFlavorCost = arr.get(i);
                int complementCost = m - currentFlavorCost;
                
                if (flavorMap.containsKey(complementCost)) {
                    return Arrays.asList(flavorMap.get(complementCost) + 1, i + 1);
                }
                
                flavorMap.put(currentFlavorCost, i);
            }
            
            return null; 
        }
    
  • + 0 comments
    def icecreamParlor(m, arr):
        for i,e in enumerate(arr):
            n = (arr[:i]+arr[i+1:])
            if m-e in n:
                return [i+1, n.index(m-e)+2]
    
  • + 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;    
    }
    
  • + 0 comments
    public static List<Integer> icecreamParlor(int m, List<Integer> arr) {
            for (int i = 0; i < arr.size(); i++) {
                if (arr.subList(i + 1, arr.size()).contains(m - arr.get(i))) {
                    return Stream.of(i + 1, arr.lastIndexOf(m - arr.get(i)) + 1).sorted().collect(Collectors.toList());
                }
            }
            
            // WTF?
            return null;
        }