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.
publicstaticintequalStacks(List<Integer>h1,List<Integer>h2,List<Integer>h3){// Write your code here// there are 3 stacks involvedList<Integer>cumulHeights=newArrayList<>();Integercumul=0;for(inti=h1.size()-1;i>=0;i--){cumul+=h1.get(i);cumulHeights.add(cumul);}cumul=0;for(inti=h2.size()-1;i>=0;i--){cumul+=h2.get(i);cumulHeights.add(cumul);}cumul=0;for(inti=h3.size()-1;i>=0;i--){cumul+=h3.get(i);cumulHeights.add(cumul);}Optional<Map.Entry<Integer,Long>>mm=cumulHeights.stream().collect(Collectors.groupingBy(s->s,Collectors.counting())).entrySet().stream().filter((s)->s.getValue()==3).max(Comparator.comparingInt(s->s.getKey()));if(mm.isPresent()){returnmm.get().getKey();}return0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Equal Stacks
You are viewing a single comment's thread. Return to all comments →
Java8 with steams