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.
public static int activityNotifications(List<Integer> expenditure, int d) {
int count = 0;
int[] freq = new int[201];
for(int i = 0; i<d; i++){
freq[expenditure.get(i)]++;
}
for(int i = d; i<expenditure.size();i++){
double median=0;
if(d%2==1){ // d/2+1 th is median
int sum = 0;
for(int j = 0; j<201; j++){
sum += freq[j];
if(sum >= d/2+1){
median = j;
break;
}
}
}
else // average of d/2 th and d/2+1 th number is median
{
int sum = 0;
int n=-1,m=-1;
for(int j = 0; j<201; j++){
sum += freq[j];
if(n<0 && sum >= d/2){
n = j;
}
if( sum >= d/2+1){
m = j;
median = (m+n)/2.0;
break;
}
}
}
if(expenditure.get(i)>=2*median)
count++;
// update freq for next window
freq[expenditure.get(i-d)]--;
freq[expenditure.get(i)]++;
}
return count;
}
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 →