Getting Runtime error in Coding Blocks Number problem

I am getting runtime error in two cases. Others are correct.
I have explained logic with comments. Please help me find why there is runtime error

    for (int i = 0; i < n;i++)
    {
        for (int j = i; j < n;j++)
        {
			int num = stoi(s.substr(i, j - i + 1));
			if(cb(num))
			{
				v.push_back(make_pair(i, j));
			}
        }
    }

you used the above code in your program.
Now just see at this line:

int num = stoi(s.substr(i, j - i + 1));

here i can be from 0 to n-1;
and say when i=0, j is from 0 to n-1;

now think of the case where,
i=0, j=n-1
now :

num=stoi(s.substr(0,n-1-0+1)
num=stoi(s.substr(0,n));
```
this is why you are getting runtime error because the elements in string are only from index 0 to n-1.


Thanks

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

Sorry for replying so late. I forgot to read this email. Thanks for looking at my code. substr takes 2 arguments starting index and number of characters from starting index including starting index so it is fine. It will simply convert the entire string into an integer. I tried it and it is not giving runtime error. There is some other problem

ohh, yess you are right. Since substr is not a routine function i forgot its parameter. you can check this code to understand the approach

Okay i understood the approach. We first try for all CB Numbers of length 1 and then 2 and so on…This is so that we can maximize number of CB numbers.
But why cant we apply greedy approach? We can sort all possible CB numbers according to whichever ends first i.e. has lower ending index (consider the substring which contains the number). Like when we try to maximize number of events that a person can attend given their starting and ending time.