Ice Cream Parlor

Sort by

recency

|

971 Discussions

|

  • + 0 comments
    public static List<Integer> icecreamParlor(int m, List<Integer> arr) {
        List<Integer> result = new ArrayList<>();
        for(int i=0;i<arr.size()-1;i++) {
            int diff = m-arr.get(i);
            int idx = arr.indexOf(diff);
            if(idx == i) {
                idx = arr.subList(i+1, arr.size()).indexOf(diff);
                if(idx != -1) idx = i+1+idx;
            }
            if(idx == -1) continue;
    
            result.add(i+1);
            result.add(idx+1);
            return result;
        }
        return result;
    }
    
  • + 0 comments

    There is inconsitent use of variable name, early on the question it named as m later on the passage, it is named as . Please be consistent.

  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/WvLuA-LCsb0

    vector<int> icecreamParlor(int m, vector<int> arr) {
        map<int, int> mp;
        for (int i = 0; i < arr.size(); i++) {
            int complement = m - arr[i];
            if (mp.count(complement)) {
                return {mp[complement] + 1, i + 1};
            }
            mp[arr[i]] = i;
        }
        return {};
    }
    
  • + 0 comments

    Python:

    def icecreamParlor(m, arr):
    	for x in range(len(arr)):
    		for y in range(len(arr)):
    			if x != y:
    				if arr[x] + arr[y] == m:
    					return (x + 1), (y + 1)
    
  • + 0 comments

    o(n) time, o(n) space My Java solution:

    public static List<Integer> icecreamParlor(int m, List<Integer> arr) {
            //goal: determine two indexes from arr that equal m when added together
            //use a map to store value and index
            Map<Integer, Integer> valueMap = new HashMap<>();
            for(int i = 0; i < arr.size(); i++){
                int price = arr.get(i); //current price
                int complement = m - price; // other price needed to equal m
                if(valueMap.containsKey(complement)){
                    List<Integer> indices = Arrays.asList(valueMap.get(complement) + 1, i + 1); // 1 based index
                    Collections.sort(indices);
                    return indices;
                } else{
                    valueMap.put(price, i); //value map didnt contain complement
                } 
            }
            return List.of(); //no valid pair was found
        }