Expected Change

Continuing the discussion from Expected Change:

Solution link: https://www.codechef.com/viewsolution/30759608
I am getting TLE in last subtasks only.
Any help will be appreciated

Creating an array of size 10^7 in Python takes enough time to cause the TLE. You can see for yourself by timing a sample run for creating an array of 1e7 size by different initialization methods.

Thank you for your reply. I saw TLE is happening because of that array initialisation.
So should I use two arrays ?or is there some way to refer to indexes of list in python without initialising them ?
I have searched extensively for the later but didn’t find anything in that context

If your implementation isn’t fast enough just go through other AC codes.
I found this AC solution.
https://www.codechef.com/viewsolution/29583603
It has a faster method to declare an array.

3 Likes

Solution link : https://www.codechef.com/viewsolution/30775676
See I have modified the code now I m not creating an array of 10^7 size but still it is giving the same result I think the problem is somewhere else.
Any help will be appreciated

I used this method to declare an array too also but still TLE in last subtests
link : https://www.codechef.com/viewsolution/30775989
I think problem is somewhere else

Wow :astonished: ! Despite using python from 7-8 months (not primarily for CP), I didn’t know about njit ! That code is really cool. Do you know any more such tricks for fast up python code (for cp) ?

No. I just looked for a 100 point submission in python for the question.

1 Like

It is not helping. Can somebody help me with my code?

submit using pypy3.

This is your code passed using pypy3.
click

Thanks a lot.
But what was the problem with python 3?

Because pypy uses in-time compiler where as cpython(standard python) uses interpreter. That means cpython first interprets code into c language and then compile it but pypy compile it directlly.

So running all python codes using pypy will run faster?
if yes why would anyone prefer python 3 over pypy?

There are various uses of cpython over pypy bcz cpython is memory efficient and it is more stable for scripts. pypy programs take huge amount of memory while compilation which makes it unstable.

Thanks a lot for helping :slight_smile: