Need help in CHEFSTEP

I am getting tle in the solution but the others solution is accepted for the same logic can some one help me
link CodeChef: Practical coding for everyone

I have used a fastReader class (by @deepthansu_1 from here) and it gives AC.

In CodeChef, you’ll have to use Fast I/O for some (read as all) questions.
If your current I/O method passes THIS test, then it is fast enough.

1 Like

I guess you’ll have to use Buffered Reader instead of Scanner.
Here is the Link for fast I/O in Java.
Also you can view other AC solutions written in Java and compare your code with those.

IN PYTHON:
for _ in range(int(input())):
n,k = map(int,input().split())
x = list(map(int,input().split()))
count = “”
for i in x:
if i % k==0:
count += “1”
else:
count +=“0”
print (count)

i think that only the FAST I/O point seems valid, as my code has the same Time Complexity (O(N)) otherwise…

#here's my PYTHON CODE:
for _ in range(int(input())):
	N,k = map(int , input().split())
	D = list(map(int , input().split()))
	ans = ""
	for i in range(len(D)):
		if(D[i] % K == 0):
			ans += "1"
		else:
			ans += "0"

https://www.codechef.com/viewsolution/36012985
This person also used Scanner but his answer was accepted.

As I have said in another thread, it’s just hard luck.

1 Like

Do not print elements one by one. Store it in a String builder object and then print your answer.

How is that a problem? I’m not fully aware of JAVA, but does print() also flush the output? If it doesn’t, then I think printing one by one should not be an issue.

For printing elements of array system.out.print() is never recommended. It is slow and using it multiple times in the code greatly affect the performance of code.
You can read more about it here. java - Difference between multiple System.out.print() and concatenation - Stack Overflow

1 Like