Hackerrank: Fraudulent Activity Notifications I'm having trouble on a question from Hackerrank, this solution works for smaller lists but seems to collapse when the list grows to 200,000 where I get the error &quot;Wrong Solution&quot;. The problem is here so you could reproduce it. Im not sure if there is an error in my code whereby it would fail. Is there anyway to improve the efficiency of my solution?
When I encounter these sort of issues where it fails at very high lists or anything of the sort, how should I react? What kind of issue should I expect to solve?
Problem text  (Incase link doesnt work):
If the amount spent by a client on a particular day is greater than or equal to 2x the client's median spending for a trailing number of days, they send the client a notification about potential fraud.
The bank doesn't send the client any notifications until they have at least that trailing number of prior days' transaction data (d) for a period of n days. Determine the number of times the client will receive a notification over all n days.
The following is the whole Hackerrank Code required to run the problem, I wrote the functions &quot;Median&quot; and &quot;activityNotifications&quot;:
#include &lt;iostream&gt;
#include &lt;algorithm&gt;
#include &lt;vector&gt;
#include &lt;bits/stdc++.h&gt;

using namespace std;

string ltrim(const string &amp;);
string rtrim(const string &amp;);
vector&lt;string&gt; split(const string &amp;);

double Median(vector&lt;int&gt; arr)
{
    int d = arr.size();

    double Even = (arr[(d/2)-1] + arr[((d/2)+1)-1]) / (double)2;
    double Odd = arr[((d+1)/2)-1];

    return (d%2==0)? Even: Odd;
}

int activityNotifications(vector&lt;int&gt; expenditure, int d) {

    int Notices = 0;

    vector&lt;int&gt; SpendData = vector&lt;int&gt;(expenditure.begin(),expenditure.end());
    sort(SpendData.begin(),SpendData.end());

    vector&lt;int&gt; SpendDataMedian = vector&lt;int&gt;(SpendData.begin(),SpendData.begin() + d);

    for (int i = d; i &lt; expenditure.size();++i)
    {
        if ((Median(SpendDataMedian)*2) &lt;= expenditure[i])
            {Notices++;}

        SpendDataMedian.erase(SpendDataMedian.begin());
        SpendDataMedian.push_back(SpendData[i]);

    }
    return Notices;
}

int main()
{
    ofstream fout(getenv(&quot;OUTPUT_PATH&quot;));

    string first_multiple_input_temp;
    getline(cin, first_multiple_input_temp);

    vector&lt;string&gt; first_multiple_input = split(rtrim(first_multiple_input_temp));

    int n = stoi(first_multiple_input[0]);

    int d = stoi(first_multiple_input[1]);

    string expenditure_temp_temp;
    getline(cin, expenditure_temp_temp);

    vector&lt;string&gt; expenditure_temp = split(rtrim(expenditure_temp_temp));

    vector&lt;int&gt; expenditure(n);

    for (int i = 0; i &lt; n; i++) {
        int expenditure_item = stoi(expenditure_temp[i]);

        expenditure[i] = expenditure_item;
    }

    int result = activityNotifications(expenditure, d);

    fout &lt;&lt; result &lt;&lt; &quot;\n&quot;;

    fout.close();

    return 0;
}

string ltrim(const string &amp;str) {
    string s(str);

    s.erase(
        s.begin(),
        find_if(s.begin(), s.end(), not1(ptr_fun&lt;int, int&gt;(isspace)))
    );

    return s;
}

string rtrim(const string &amp;str) {
    string s(str);

    s.erase(
        find_if(s.rbegin(), s.rend(), not1(ptr_fun&lt;int, int&gt;(isspace))).base(),
        s.end()
    );

    return s;
}

vector&lt;string&gt; split(const string &amp;str) {
    vector&lt;string&gt; tokens;

    string::size_type start = 0;
    string::size_type end = 0;

    while ((end = str.find(&quot; &quot;, start)) != string::npos) {
        tokens.push_back(str.substr(start, end - start));

        start = end + 1;
    }

    tokens.push_back(str.substr(start));

    return tokens;
}

Sample input is:
9 5
2 3 4 2 3 6 8 4 5

Expected output is:
2
Yes, deciding for each day in isolation is easy enough. And produces reusable and well-defined abstractions.
Unfortunately, it costs you. Specifically, it needs too long. Use a running-total, thus each day needs only subtracting one trailing day, adding the last day, and comparing to that day. The cost is pretty independent of the number of trailing days considered. It is easily adapted to streams.
Additionally, you really should avoid floating-point, not necessarily due to its runtime cost, but its design cost. Or do you have the time and inclination to prove conclusively whether your handling of floating point rounding is right?
I won't go into all the bad habits hackerrank encourages, like using &lt;bits/stdc++.h&gt;, using namespace std;, all the useless copies, ...
If you are interested, it's repeated enough times already.