Guys, I’m totally new to vectors and STL and I’m getting an error in this code so please help me out.

https://www.codechef.com/viewsolution/34630704

Thanks

You use 0-based indexing whereas you’re supposed to use 1-based indexing for this (as per the question).

You’ve declared a vector of size 100. This means that the indices are in the range [0, 99]. Now you take in `pos`

. What if `pos`

is 100? It will be out of bounds because the vector can only contain till 99. So if you try to access an out of bounds element, you get the SIGABRT RE.

My AC code (notice how I declare it with 101 rather than 100 so I can get 100 as the last index because I only care about indices [1, 100] so I ignore 0):

```
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
{
int m, x, y, c;
cin >> m >> x >> y;
vector <int> h(101, 0), cops(m);
for (int i = 0; i < m; ++i) cin >> cops[i];
for (int i = 0; i < m; ++i)
{
for (int j = cops[i]; j <= min(100, cops[i]+x*y); ++j) h[j] = 1;
for (int j = cops[i]; j >= max(0, cops[i]-x*y); --j) h[j] = 1;
}
cout << count(h.begin()+1, h.end(), 0) << '\n';
}
}
```

1 Like

Thanks man