Ice Cream Parlor

  • + 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;
    }