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>cutTheSticks(List<Integer>arr){/* array hai > sort >iterate 0 to n-1, store count of each ineteger in a map. now iterate 0 to n-1 with temp initially=0, count=arraysize add count-temp to result. as you move through the array's each element, check if it has changed or not. if element changes, temp = map.get(oldElement) contact mrayyankarimi@gmail.com without subject, only text so I know it's not some automated company mail */Collections.sort(arr);inttemp=0,s=arr.size(),count=s;List<Integer>res=newArrayList();HashMap<Integer,Integer>m=newHashMap<>();for(inti=0;i<s;i++){intx=arr.get(i);m.put(x,m.getOrDefault(x,0)+1);}res.add(count);intoldElement=arr.get(0),i=1;while(i<s&&arr.get(i)==oldElement)i++;for(;i<s;i++){if(arr.get(i)!=oldElement){temp+=m.get(oldElement);count=s-temp;res.add(count);oldElement=arr.get(i);}}/* 1 1 2 2 3 3 3 4 e=8 6 4 1 8 6 4 1 */returnres;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Cut the sticks
You are viewing a single comment's thread. Return to all comments →
i hate coding but money
Java