TLE . How can i optimize this better?

This is my code in python for the Enormous Input Test problem :

n,k=map(int, raw_input().split())
count=0
while n>0 :
	x=int(raw_input())
	if x%k==0 :
		count=count+1
	n-=1
print count

How can i optimize this in a better way or what am doing wrong? i’m facing this problem in several of my python codes.Is there any way i can overcome this TLE problem?

2 Likes

import sys module and use sys.stdin.readline().strip() for raw_input() and use sys.stdout.write() for print. :slight_smile:

2 Likes

@akarsh_hedge try importing the sys module for faster input output , it has always helped me , hope it helps you too .

You would clearly see the time difference on using sys .

Happy Coding :slight_smile:

6 Likes

I agree with both @its_pheonix and @vaibhav9518s , importing sys will surely help you .

1 Like

thank u . will try that right away! :slight_smile:

I am having same problem. I have tried both input() and sys.stdin.readline(). still TLE. This is also keeping me from earning full points on some competition problems as I believe my algorithms are often good save for i/o issues. This is frustrating as it seems as if Python submissions are being accepted they should be accommodated… otherwise wby accept them…

Here is my code for enormous input test:

import sys
n, k = [int(i) for i in sys.stdin.readline().strip().split()]
c = 0
for i in range(n):
if int(sys.stdin.readline().strip()) % k == 0:
c += 1
print©