# Codeforces Round 685 DIV-2 Non-Substring Subsequence

So in this codeforces round 685 i solved the B problem. It passed all the pretests initially.However later it showed a runtime error in the system run. Can someonehelp me with my solution?
[Problem][https://codeforces.com/contest/1451/problem/B]
Solution:
#include
#include <bits/stdc++.h>
using namespace std;

int main() {int t;
cin>>t;
while(t–)
{int n,q,i;
char str[n+1];
cin>>n>>q;
cin>>str;
while(q–)
{
int l,r,k=0;
cin>>l>>r;
for(i=0;i<=l-2;i++)
{
if(str[i]==str[l-1])
{k++;

break;}
}
for(i=r;i<=n-1;i++)
{
if(str[i]==str[r-1])
{k++;

break;}
}
if(k==0)
cout<<"NO"<<endl;
else
cout<<"YES"<<endl;

}

}
return 0;
}

I used similar approach
my code : https://codeforces.com/contest/1451/submission/99170194
it worked for me

simplify your code a little bit. decrease l and r beforehand so that they become valid indices.
The 2nd for loop going from r to n-1 can possibly have some bugs…

1 Like

run the 2nd loop from r+1 to n-1

1 Like

You are runing for loop for every query it will increase your time complexity.

1 Like

Actually I rectified my solution since char array is not very efficient I changed it to string and the solution worked. Thank you for your idea.

Rectified the solution the problem was I was using a character array instead of string.

Well there wasn’t any bug but i used string data type instead of a character array and it was all done

1 Like
while(t--){
ll n,q;
cin>>n>>q;
string s;
cin>>s;
while(q--){
ll l,r,flag=0,i;
cin>>l>>r;
l--;
r--;
for(i=r+1;i<n;i++){
if(s[i]==s[r]){
flag=1;
}
}
for(i=0;i<=l-1;i++){
if(s[i]==s[l]){
flag=1;
}
}
if(flag==1){
cout<<"YES";
}
else{
cout<<"NO";
}
cout<<endl;
}
}

If L’th character is present before index L , OR , R’th character is present after index R then answer is “YES” else “NO”

1 Like

// #include <math.h>

using namespace std;

int main(){

int t;

cin>>t;

while(t--)

{

//ios::sync_with_stdio(false); cin.tie(0);

int n,q;

cin>>n>>q;

string s;

cin>>s;

while(q--)

{

int l,r;

cin>>l>>r;

//if(r>n)

//cout<<"no"<<endl;

if(l==r)

cout<<"no"<<endl;

else

{

string s1=s.substr(l-1,(r-l)+1);

int ans=s1.find(s[0]);

int c=0;

//cout<<s1<<" "<<ans<<endl;

int i=ans+2;

//cout<<i<<endl;

//cout<<s1<<endl;

for(int j=1; j<s1.length(); j++)

{

if (s.find(s1[j],i) != string::npos)

{

c=1;

//cout<<s1[j]<<" "<<i<<endl;

}

else

{

c=0;

break;

}

i++;

}

if(c==1)

cout<<"yes"<<endl;

else

cout<<"no"<<endl;

}

}

}

return 0;

}
what is wrong with my code it's says WA