c650/hackerrank-solutions

issue with a test case

rajatb115 opened this issue · 0 comments

Hi, this is a brilliant solution but I was getting some difficulty while running a specific test case using your code.

Test case

5 3
10 20 30 40 50

I went through the code and came up with the new solution Please review my solution

#include
#include
#include

using namespace std;

int main(void) {
int n,d;
cin >> n >> d;

vector<int> days(n);
for (auto& e : days)
    cin >> e;

vector<size_t> freq(201);
size_t i = 0;
for (; i < d; ++i)
    ++freq[days[i]];
// till here we are doing

for (size_t j = 1; j < freq.size(); ++j)
    freq[j] += freq[j-1];

size_t cnt = 0;
double tmp;

size_t j;
while(i < n) {
    for (j = 0; j < freq.size() && freq[j] <= d/2; ++j);
    
    //cout<<"j="<<j<<" freq[j]="<<freq[j]<<"\n";
    //  freq[j] >= d/2
   if (freq[j-1] == d/2) {
        tmp = j;
        //cout<<"#temp="<<tmp<<" \n";
        if (d % 2 == 0) {
            for(int k=j-1;k>0;k--){
                if(freq[k]<d/2){
                    //cout<<"#freq[k]="<<freq[k]<<"\n";
                    tmp+=k+1;
                    break;
                }
            }
            tmp /= 2;
        }
    }

    else {
        tmp = j;
        if (d % 2 == 0) {
            tmp += freq[j-1] + 1 == freq[j] ? j-1 : j;
           tmp /= 2;
        }
    }

    tmp *= 2; 
    //cout<<"temp="<<tmp<<" ";

    cnt += days[i] >= tmp;

    for (j = days[i-d]; j < freq.size(); ++j)
        --freq[j];
    for (j = days[i]; j < freq.size(); ++j)
        ++freq[j];
    
    ++i;
}

cout << cnt << '\n';

return 0;

}