We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
publicstaticList<Integer>icecreamParlor(intm,List<Integer>arr){//goal: determine two indexes from arr that equal m when added together//use a map to store value and indexMap<Integer,Integer>valueMap=newHashMap<>();for(inti=0;i<arr.size();i++){intprice=arr.get(i);//current priceintcomplement=m-price;// other price needed to equal mif(valueMap.containsKey(complement)){List<Integer>indices=Arrays.asList(valueMap.get(complement)+1,i+1);// 1 based indexCollections.sort(indices);returnindices;}else{valueMap.put(price,i);//value map didnt contain complement}}returnList.of();//no valid pair was found}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Ice Cream Parlor
You are viewing a single comment's thread. Return to all comments →
o(n) time, o(n) space My Java solution: