Please someone help me with the runtime error of this code


#1

#include <bits/stdc++.h>
vector Solution::slidingMaximum(const vector &A, int B) {
int t;
vector max;
for(t=0;t+B<A.size();t++)
{
max[t]=int(*max_element(A.begin()+t,A.begin()+t+B));

}
return max;

}


#2

The size of the vector max is zero.
so u can’t access any element of the vector max. In order to access elements of the vector u should allocate memory prior to accessing the elements. So give some initialize size n to it such that t < n. so that whenever u try to access t, that particular index is present in the vector. In your case t-th element is not there since size if 0.
do something like this.
int x = int(*max_element(A.begin()+t,A.begin()+t+B));
max.push_back(x);