I tried to solve a question in Hackerrank. I passed all the sample test cases but couldn't pass all the hidden test cases due to the "exceeded time limit" error. The link to the problem question is: Fraudulent-activity notification
My code is as follows.
#include <bits/stdc++.h>
using namespace std;
vector<string> split_string(string);
// Complete the activityNotifications function below.
int activityNotifications(vector<int> v, long int d) {
long int n,ex;
long double m;
long int cap=v.size();
long int notif=0;
n=d;
for(long int i=0;i<n&&n<cap;i++,n++)
{
ex=v[n];
sort(v.begin()+i, v.begin()+i+d);
if(d%2==0)
{
m=(v[(n+i-1)/2]+v[(n+i)/2])/2.0;
}
else
{
m=v[(n+i)/2];
}
if((m*2)>=ex)
{
notif++;
}
}
return (notif-1);
}
int main()
{
vector<int>v;
long int n,d;
long int item;
cin>>n>>d;
for(long int i=0;i<n;i++)
{
cin>>item;
v.push_back(item);
}
int res=activityNotifications(v,d);
cout<<res;
return 0;
}
How can I optimize this code? Please help.