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 "Wrong Solution". 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 "Median" and "activityNotifications":
#include <iostream>
#include <algorithm>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
string ltrim(const string &);
string rtrim(const string &);
vector<string> split(const string &);
double Median(vector<int> 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<int> expenditure, int d) {
int Notices = 0;
vector<int> SpendData = vector<int>(expenditure.begin(),expenditure.end());
sort(SpendData.begin(),SpendData.end());
vector<int> SpendDataMedian = vector<int>(SpendData.begin(),SpendData.begin() + d);
for (int i = d; i < expenditure.size();++i)
{
if ((Median(SpendDataMedian)*2) <= expenditure[i])
{Notices++;}
SpendDataMedian.erase(SpendDataMedian.begin());
SpendDataMedian.push_back(SpendData[i]);
}
return Notices;
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
string first_multiple_input_temp;
getline(cin, first_multiple_input_temp);
vector<string> 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<string> expenditure_temp = split(rtrim(expenditure_temp_temp));
vector<int> expenditure(n);
for (int i = 0; i < n; i++) {
int expenditure_item = stoi(expenditure_temp[i]);
expenditure[i] = expenditure_item;
}
int result = activityNotifications(expenditure, d);
fout << result << "\n";
fout.close();
return 0;
}
string ltrim(const string &str) {
string s(str);
s.erase(
s.begin(),
find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
);
return s;
}
string rtrim(const string &str) {
string s(str);
s.erase(
find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
s.end()
);
return s;
}
vector<string> split(const string &str) {
vector<string> tokens;
string::size_type start = 0;
string::size_type end = 0;
while ((end = str.find(" ", 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