While solving a codeforces problem, I had to make a vector of size=1. Also, I needed to iterate back from the second last element of the vector, so I used the following technique to use for loop.
for(int i = vec.size()-2; i > -1; i--){
vec[i] = vec[i] + vec[i+1];
}
This technique throws runtime error
at codeforces compiler.
But, using the size of the vector by precalculating it, it runs fine.
Following snippet runs successfully.
int s = vec.size();
for(int i = s-2; i > -1; i--){
vec[i] = vec[i] + vec[i+1];
}
Can someone help me understand this phenomenon?
PS: While figuring out the issue, I did
cout << vec.size()-2;
and to my surprise, the output came out to be
18446744073709551615
when the vector size was 1. The output should have been -1, right? Is this obvious, or something else. Kindly explain.