Hash Tables: Ice Cream Parlor

  • + 0 comments
    public static void whatFlavors(List<Integer> cost, int money) {
    // Write your code here
    Map<Integer, List<Integer>> costByIndex = new HashMap<>();
    for(int i =0;i<cost.size();i++){
        int balance = money-cost.get(i);
        List<Integer> ids;
        if(costByIndex.containsKey(balance)){
            ids = costByIndex.get(balance);
        }else ids = new ArrayList<>();
        ids.add(i+1);
        costByIndex.put(balance, ids);
    }
    
    
     for(int i =0;i<cost.size();i++){
        int balance = cost.get(i);
         if(costByIndex.containsKey(balance)&& (costByIndex.get(balance).get(0)!=(i+1))){
            int val1 = Math.min(i+1,costByIndex.get(balance).get(0));
            int val2 = Math.max(i+1,costByIndex.get(balance).get(0));
            System.out.println(val1+" "+val2);
            break;
        }
    }