Why my code is giving TLE ? (CHEFSTEP)

I have tried the same approach to solve @CHEFSTEP which almost everyone had given in the contest but my c++ solution is giving TLE, I don’t know why?
And today when I did it with python the solution got accepted…
I wanted to know my mistake so that I may not repeat it…Please help.
My C++ code:-
[CodeChef: Practical coding for everyone]

My Python Code which got accepted:-
[CodeChef: Practical coding for everyone]

You have to use fast I/O for C++.

ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);

Add this at the beginning of your main() and your C++ code will pass.

Python is comparatively slower than C++. So CodeChef gives a higher time limit for Python.

3 Likes

Can we use this in each and every cpp code ?

Yes. But don’t use it for interactive problems. And if you’re using this, do not use C style printing functions like puts() or printf(). This will mess up the order in which things are printed.

Don’t use this for interactive problems, because you’ll have to flush the output at each and every step, which the judge reads and gives corresponding information.

1 Like

There are no mistakes. He does take the input in the for loop.

1 Like

Thanks MAN !!

Did they change the input format today? Yesterday it was not in a single line, but today all the steps were in a single line

I did not participate yesterday. I was participating in hackercup. So I’m not sure.

But they don’t change the format like that. That’s very strange if they have done it.

1 Like

Just Noticed! My bad

1 Like

It doesn’t matter in C++ if there is every input in one line seperated by a space or given in a new line . Can’t say about this format in other languages
e.g. 1 5 3 12 13 18 20 27216 this will also give the same output in CHEFSTEP problem

Can u help me up with BINFUN problem?

I guess that it is true for any language. The only problem is when you use int a[] = sc.readArray() in JAVA, (assuming you have a readArray() function in your reader class) or a = list(map(int, input().split())) in Python.

There shouldn’t be a problem even in JAVA and Python if we use a loop to read the next integer.

@nme8 Have you gone through this. It might help.

so why this fast io is needed here…any reason for this?..

The input for this question is quite large, and traditional input methods take time reading it. If the time limit was 2 seconds, I guess fast input won’t be required.

2 Likes

Sir,could you please tell me How to improve my ratings?? As I am trying my best to do so,but if you check my profile once my ratings is going worse day by day…

but why this solution work even without fast i/o ?

#include
using namespace std;

int main() {
// your code goes here
int t, n, k, i;

cin>>t;
while(t--)
{
    cin>>n>>k;
    int a[n];
    
    for(i=0 ; i<n ; i++)
    {
        cin>>a[i];
    }
    
    for(i=0 ; i<n ; i++)
    {
        
        if(a[i]%k==0)
        cout<<"1";
        else
        cout<<"0";
    }
    cout<<endl;
}

return 0;

}

1 Like

Bro my solution is exactly the same

We cant really say what happens while running. Probably yours took 0.9 seconds and just passed the limit, while his would’ve finished at 1.1 sec, but it was stopped right after 1.0 sec and given a TLE verdict.

@dashing123 I would say, don’t worry about your rating right now. Just keep solving contests and problems and keep learning. Your rating will automatically improve in the future.

3 Likes

Yes they did, first I tried in Python I was getting NZEC later in CPP I was getting TLE.