This isn't an answer, but I'm building on certain things that
and
have said.
I assume you're familiar with a vector object having an automatic growth in size every time you insert a piece of data into a vector that spills over in .size(). You gave us an example of your vector having these elements: {1,1,1,1,1,2,2,2,2,2,2} with eleven(11) elements inside of it. Correct me if I'm wrong on the counts, but the point will stay the same.
Now let's walk through your for loop: let's say we're at the last step, where
1 2 3
|
x = 10
// therefore, your code does its stuff and x increments
// now the problem becomes arr[10]==arr[11]
|
As you now may have noticed, what's supposed to be in the 12th slot of your vector "arr"?
arr[10] will refer to your last 2 in the example data.
As a side note, I probably wouldn't name a vector as "arr". IMO that's just horrible naming sense. Typically, you'd want to note what an object or a function is used for, like getInt(), or getNonNegativeInt() or other similar functions, with parameters inside like upperBound, limit, etc. Also not mentioning logical size vs physical size.