In december long challenge there were few questions which i did and were partially correct as my code was not time efficient and TLE was executed. i want to know how to write codes to remove TLE and need help. any type of suggestion,advice is welcomed.

i am sharing my code of particular problem it is executing TLE due to 2 for loops inside while loop ,but i only know this .how to improve that i want to know

here is one problem(SUBSPLAY):- https://www.codechef.com/DEC19B/problems/SUBSPLAY

my solution:-

```
using namespace std;
int main()
{
int t;
//cout<<"enter total number of test cases ";
cin>>t;
while(t--)
{
int N;
// cout<<"enter string length ";
cin>>N;
string S;
// cout<<"enter string ";
cin>>S;
int i,j,c,K=0,d=0,min,found;
min=N; //storing size of string in min
for(i=0;i<S.length();++i) //loop from start of string 1st character to end
{
c=count(S.begin(),S.end(),S[i]); //counting number of character similar to S[i]
if(c==N) //if all charcters of string are same
{
min=1;
}
if(c>1&&c!=N) //if character is not unique and all charcters of string are not same
{
for(j=i+1;j<N;j++) //another loop from i+1 to N-1 for finding minimum difference of indices
{
if(S[i]==S[j]) //checking similar charcters
{
d=j-i; //difference of indices
if(min>d)
{ min=d;
break;
}
}
}
}
}
//K will be length of string - minimum difference of indices of similar charcters
K=N-min;
cout<<K<<endl;
}
return 0;
}```
```