your for loop is starting from i=0,which gives runtime error when you access the index
“dp[i-1]” because “0-1” is index out of bounds which is wrong.
hope this helps 
I used the same approach yesterday but in python. I tried to submit my code in Python 3 , PYPY3 and finally in CPYTHON(2.7) but all gave TLE.
Link to Python Solutions:
Today I wrote the same code in c++ and it worked (to my surprise in 0.13 sec)
Link to C++ solution:
https://www.codechef.com/viewsolution/25396226
Can anybody explain what was that? Any help would be appreciated.
I have solved the problem using sliding Window but I am getting a TLE
Here is the link to my Solution
link to Solution
i cannot understand the approach can any body tell me??
I would be helpful if you can explain the conditions in your second loop
I am surprised here ![]()
Let me see if anything is wrong. This is what it might happen if author keep TL tight. People complains that wrong solutions are passing if he keeps it lenient. But I am more sad when I see this type of solution not passing ( if I am not wrong that this is correct approach) .
@rajan_jha
Anyways CodeChef: Practical coding for everyone
Here is your solution with AC. Just did a small change. ( removed multiplication since its a costly operation).
You should feel like “lesson learnt, don’t use multiplication unless necessary in such ques”.
@l_returns
Thanks for the help and a very well lesson learned indeed
But the TL was very tight
Also in the editorial it has been mentioned that the problem can be solved either by using sliding window or by prefix table but none of the solutions either setter’s or tester’s is using sliding window otherwise that would have helped.
Can someone tell me why am i getting a runtime error for this solution.
Thanks in advance
https://www.codechef.com/viewsolution/25407101
just dry run the code and you will understand it by yourself…
@l_returns
Thanks a lot for pointing out the teeny tiny liitle optimisation. I was also doing the unnecessary multiplication and getting a TLE. Your hack is sleek and elegant.
yeah , i was copying that full dictionary, now i have changed my code a bit, but it is still timing out, please have a look.
Welcome
\hspace{0mm}
I am getting Wrong answer with my code but most of the test cases gave correct answer.Someone help pls !
#include <bits/stdc++.h>
using namespace std;
int main() {
int t; cin>>t;
while(t--)
{
string s; cin>>s;
int n = s.length() , cnt1=0 ;
for(int i=0;i<n;++i)
{
if(s[i]=='1')
cnt1++;
s[i]=cnt1 + '0';
}
int ss=0,times=0;
for(int i=1; i<=cnt1 && (i*i + i)<=n ; ++i)
{
int k= i + (i*i) ;
ss= s[k-1] - '0';
if(ss==i)
times++;
for(int z=1; z<= n-k ;++z)
{
ss = s[z+k-1] - s[z-1] ;
if(ss==i)
times++;
}
}
cout<<times<<endl;
}
return 0;
}
I have a similar approach as the editorial,can anyone tell me why it’s TLE?
https://www.codechef.com/viewsolution/25413907
try writing "System.out.println(e); " in the catch block
I tried implementing the approach mentioned but i get time limit exceeded.
Can u suggest any improvements???
//MY CODE:
#include
#include<math.h>
using namespace std;
int main() {
long T;
cin>>T;
while(T–)
{
string s;
cin>>s;
long size=s.size(),x=1,ans=0;
for(x;x<=pow(size,0.5);x++)
{
long req_size=x*x+x,cnt1=0,i=0,j=i+req_size-1;
if(req_size>size){break;}
for(i=0;i<req_size;i++,j++)
{
if(s[i]=='1'){ cnt1++;}
}
i--;j--;
if(cnt1==x){ans++;}
while(i<=s.size()-req_size)
{
if(s[i-1]=='1'){cnt1--;}
if(j<size && s[j]=='1'){cnt1++;}
if(cnt1==x){ans++;}
i++;j++;
}
}
cout<<ans<<endl;
}
return 0;
}
Why he(@taran_1407 ) did len = i*i+i,
As expained x is count of 1’s, he is using i (index/position)??
I’m confused here, there should be count of 1’s???
I used the sliding window method, but still I’m getting a TLE. Here’s my code. Can someone please help?
@raisinten
Refer this solution
https://www.codechef.com/viewsolution/25406557
Replace the multiplication operator inside for loop with the comparison operator as multiplication is an expensive operation in terms of the time consumed and you will be able to get rid of the TLE
Thank you so much! Solved it here.
I knew that multiplication is really slow, but I didn’t know that it would slow my code down so much. Is compiler optimization not at play here? Thank you. ![]()