2
int n;//input size of array
cin >> n;
vector <int> a(n);
vector <int> in;

for (int i = 0; i < n; i++)
    cin >> a[i];//input array elements
if (n == 1) {
    cout << "1" << "\n";
    return 0;
}

for (int i = 1; i <= n ; i++)//to get longest incresing subsequence in the array
{
    int flag = 0, j = i;

    while (j < n && a[j] >= a[j - 1] ) {
        j++;
        flag = 1;
    }
    if (flag == 1) {
        in.push_back(j - i + 1);
        i = j;
    }
}

int maxval = in[0]; //to get maximum sized element from in 
for (int i = 1; i < in.size(); i++)
    if (in[i] > maxval)
        maxval = in[i];
cout << maxval << "\n";

I tried the same code for values < 10000 it's working fine...i've replaced all int's by long long int's then also it's showing vector subscript out of range error...

Sample input :

10

49532 49472 49426 49362 49324 49247 49165 49162 49108 49093

i'm expecting 0 but it shows "vector subscript out of range"

9
  • 9
    Make this mistake often enough and you'll learn that <= in looping code is always a code-smell. Commented Nov 11, 2019 at 16:28
  • 3
    i'm expecting 0 but it shows "vector subscript out of range" -- What is the issue understanding this error? The error is self-explanatory. Commented Nov 11, 2019 at 16:31
  • 2
    Aside: the final loop can be replaced by std::max_element (found in <algorithm>) Commented Nov 11, 2019 at 16:35
  • 2
    "i'm expecting 0" Explain why. Commented Nov 11, 2019 at 16:37
  • 2
    Honestly, I don't know why this is getting upvoted. The error should be obvious in terms of what it is stating. Commented Nov 11, 2019 at 16:56

2 Answers 2

7

The reason of the problem is this statement

int maxval = in[0];//to get maximum sized element from in 

The vector in is empty when this input is used

10

49532 49472 49426 49362 49324 49247 49165 49162 49108 49093

So you may not use the subscript operator.

You could write for example

int maxval = in.empty() ? 0 : in[0];
Sign up to request clarification or add additional context in comments.

1 Comment

alternatively, they could change if (flag == 1) { in.push_back(j - i + 1); i = j; } to in.push_back(j - i); i = j;
2

Fix this:

int maxval = in.size()? in[0]:0;

the vector class operator checks the index is between lower and upper limit of array which will be (0 -> size-1)

MSVC lib:

    _NODISCARD _Ty& operator[](const size_type _Pos) noexcept { // strengthened
        auto& _My_data = _Mypair._Myval2;
#if _CONTAINER_DEBUG_LEVEL > 0
        _STL_VERIFY(
            _Pos < static_cast<size_type>(_My_data._Mylast - _My_data._Myfirst), "vector subscript out of range");
#endif // _CONTAINER_DEBUG_LEVEL > 0

        return _My_data._Myfirst[_Pos];
    }

the problem is that the in.push_back never get called then the size is zero.

so in[0] call operator will throw exception index out range.

6 Comments

An answer should explain what was wrong and how it resolves that. Anyway, this just repeats what Vlad already wrote, in a less clear way. (I didn't downvote, fwiw.)
"vector class operator checks the index" operator[] does not have to check anything. The fact that it sometimes does in debug builds produced by some compilers should not be relied upon. If an index might be out of bounds at runtime and is not controllable, the user must use .at() to ensure that the index is checked and an exception thrown if invalid. operator[] makes no guarantee and is free to turn your entire program into undefined behaviour if it receives an invalid index, so only users who are confident what they are doing should use it.
you are right but he is getting exception and here is the operator definition:
_NODISCARD _Ty& operator[](const size_type _Pos) noexcept { // strengthened auto& _My_data = _Mypair._Myval2; #if _CONTAINER_DEBUG_LEVEL > 0 _STL_VERIFY( _Pos < static_cast<size_type>(_My_data._Mylast - _My_data._Myfirst), "vector subscript out of range"); #endif // _CONTAINER_DEBUG_LEVEL > 0 return _My_data._Myfirst[_Pos]; }
so he is building debug build
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.