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.
Java 8 - Used an Index Sort to order the values. One time through the list and to find the correct pair.
publicstaticList<Integer>icecreamParlor(intm,List<Integer>arr){List<Integer>rtn=newArrayList<>();// Index Sortint[]indices=IntStream.range(0,arr.size()).boxed().sorted(Comparator.comparingInt(i->arr.get(i))).mapToInt(Integer::intValue).toArray();// Close in on the solutionintl=0;inth=arr.size()-1;while(l<h&&arr.get(indices[l])+arr.get(indices[h])!=m){intvh=arr.get(indices[h]);intvl=arr.get(indices[l]);if(vh>=m||(vh+vl)>m)--h;else++l;}// sort result into asscending order.rtn.add(Math.min(indices[l],indices[h])+1);rtn.add(Math.max(indices[l],indices[h])+1);returnrtn;}
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 →
Java 8 - Used an Index Sort to order the values. One time through the list and to find the correct pair.