Problem with code

why does this code works fine
class Solution {
public:
bool threeConsecutiveOdds(vector& arr) {
for(int i=0;i+2<arr.size();i++)
{
if(arr[i]&1 and arr[i+1]&1 and arr[i+2]&1)
return true;
}
return false;
}
};
and then this code gets runtime error
class Solution {
public:
bool threeConsecutiveOdds(vector& arr) {
for(int i=0;i<arr.size()-2;i++)
{
if(arr[i]&1 and arr[i+1]&1 and arr[i+2]&1)
return true;
}
return false;
}
};

Share your code with proper indentation.
size() or length() methods return an unsigned integer because size of array/string can’t be negative. When size is 1 it will overflow back to positive integer instead of becoming -1.
The proper way to do it is with casting.

for(int i=0;i<(int)arr.size() - 2; i++)