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.
Here is a Java solution with sorted arrayList and sliding window:
publicstaticintactivityNotifications(List<Integer>expenditure,intd){varlist=newArrayList<Integer>();intnotifications=0;Function<List<Integer>,Double>medFunc;if(d%2==0){medFunc=(arr)->((arr.get(d/2)+arr.get(d/2-1))/2.);}else{medFunc=(arr)->(double)arr.get(d/2);}varindexToRemove=0;for(vari=0;i<expenditure.size();i++){if(i<d){add(expenditure.get(i),list);}else{doublemed=medFunc.apply(list);if(med*2<=expenditure.get(i)){notifications++;}if(i<expenditure.size()-1){add(expenditure.get(i),list);remove(expenditure.get(indexToRemove),list);indexToRemove++;}}}returnnotifications;}publicstaticvoidadd(Integervalue,List<Integer>list){intindex=Collections.binarySearch(list,value);// If value is not found, binarySearch returns (-(insertion point) - 1)if(index<0){index=-(index+1);}list.add(index,value);}publicstaticvoidremove(Integervalue,List<Integer>list){intindex=Collections.binarySearch(list,value);if(index>=0){list.remove(index);}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Fraudulent Activity Notifications
You are viewing a single comment's thread. Return to all comments →
Here is a Java solution with sorted arrayList and sliding window: