Hash Tables: Ice Cream Parlor

  • + 3 comments

    here is my java solution. short and sweet.

     static void whatFlavors(int[] cost, int money) {
            Map<Integer,Integer> map = new HashMap<>();
            map.put(money-cost[0],1);
            for(int i=1; i<cost.length; i++) {
                if(map.containsKey(cost[i])) {
                    System.out.println(map.get(cost[i])+" "+(i+1));
                    break;
                }
                else if(!map.containsKey(cost[i])&&money-cost[i]>0) {
                    map.put(money-cost[i], i+1);
                }
            }
    
        }