Ice Cream Parlor

Sort by

recency

|

967 Discussions

|

  • + 0 comments

    Hash map

    1. Time Complexity: O(n)
    2. Space Complexity: O(n)
    int* icecreamParlor(int m, int arr_count, int* arr, int* result_count) {
        *result_count = 2;
        int* res = (int* )malloc((*result_count)*sizeof(int));
        int* map = (int* )calloc(10001,sizeof(int));
        for (int i = 0; i<arr_count; i++){
            int temp = m-arr[i];
            if (temp >= 0 && map[temp] != 0){
                res[0] = map[temp];
                res[1] = i+1;
                free(map);
                return res;
            }
            map[arr[i]] = i+1;
        }
        return NULL;
    }
    
  • + 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

    Solution in Python with time complexity O(n)

    def icecreamParlor(m, arr):
        indices = []
        prices = {}
        for i, p in enumerate(arr):
            compl = m - p
            if compl in prices:
                indices = [prices[compl] + 1, i + 1]
            prices[p] = i
        print(prices)
        return sorted(indices)
    
  • + 0 comments

    Here is my Python solution!

    def icecreamParlor(m, arr):
        for f1 in range(len(arr)):
            for f2 in range(f1 + 1, len(arr)):
                if arr[f1] + arr[f2] == m:
                    return sorted([f1 + 1, f2 + 1])
    
  • + 0 comments

    Solution in Java:

            Map<Integer, Integer> map = new HashMap<>();
    
            for (int i = 0; i < arr.size(); i++) {
                int complement = m - arr.get(i);
    
                if (map.containsKey(complement)) {
                    return Arrays.asList(map.get(complement) + 1, i + 1);
                }
    
                map.put(arr.get(i),i);
            }
    
            return Arrays.asList(-1, -1); 
    }