Ice Cream Parlor

Sort by

recency

|

78 Discussions

|

  • + 0 comments

    Javascript Solution

    function icecreamParlor(m, arr) {
      for (let i = 0; i < arr.length; i++) {
        const remainder = m - arr[i];
        const secondIndex = arr.indexOf(remainder);
        if (secondIndex != -1 && secondIndex != i) {
          return [i+1, secondIndex+1].sort((a,b) => a - b);
        }
      }
    }
    
  • + 0 comments
    public static List<Integer> icecreamParlor(int m, List<Integer> arr) {
    // Write your code here
        Map<Integer, Integer> costVsIndex = new HashMap<>();
        List<Integer> result = new ArrayList<>();
        for(int i = 0; i< arr.size(); i++) {
            Integer remaining = m - arr.get(i);
            if(costVsIndex.containsKey(remaining)) {
                 result.add(costVsIndex.get(remaining) + 1);
                 result.add(i + 1);
            } else {
                costVsIndex.put(arr.get(i), i);
            }
        }
        return result;
    }
    

    }

  • + 0 comments

    I'll never understand the HackerRank trend of posting solutions in the discussion section let alone uncommented and unexplained solutions.

    My thoughts: * I considered a two-pointer solution but the array isn't pre-sorted so this would require a sort which adds n * log(n) or a custom sort/search

    • Alternatively, you can use memoisation (collect some info while traversing for later). You need to think about the complement of the current price and available money.

    • Don't forget that the array isn't unique, just the solution

  • + 0 comments
    List<Integer> temp;
        List<Integer> res=new ArrayList<>();
        for(int i=1;i<arr.size();i++){
            temp=arr.subList(i,arr.size());
            if(temp.indexOf(m-arr.get(i-1))!=-1){
                res.add(i);
                res.add(temp.indexOf(m-arr.get(i-1))+i+1);
                return res;
            }
        }
        return res;
    
  • + 0 comments

    Java Solution

        public static List<Integer> icecreamParlor(int m, List<Integer> arr) {
            
            List<Integer> list = new ArrayList<>();
            for(int i=0; i< arr.size()-1; i++){
                for(int j=i+1; j<arr.size(); j++){
                    if(arr.get(i) + arr.get(j) == m){
                        list.add(i+1);
                        list.add(j+1);
                    }
                }
            }
            
            return list;
        }