RAAVANCH-TLE

https://www.codechef.com/problems/RAAVANCH
I know the right answer for this question but my problem is with my wrong solutions.I have submitted the same code in c++ and java which gave me wa and tle respectively.
C++ code:https://www.codechef.com/viewsolution/33156872
java code:https://www.codechef.com/viewsolution/33156464
So can someone tell me why this occured?

First of all, this solution will not give you TLE but will definitely be wrong as you are not keeping the track of range of i+k-1 in the inner for loop.

Try a test case as:
1
5 4 4

Whenever inner for loop runs in your solution it doesn’t follow any bound so you get some extra zeros printed. You can modify your solution and bound the values of i+j in the inner loop as given below.

#include <iostream>
#include<bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int t;
	cin>>t;
	while(t--) {
		int n,k;
		cin>>n;
		cin>>k;
		long long int x;
		cin>>x;
		int i,j;
		for(i=0;i<n;i++) {
			cout<<x<<" ";
			for(j=1;j<k;j++) {
			    if(i+j>n-1)
			        break;
				cout<<0<<" ";
			}
			i=i+j-1;
		}
		cout<<"\n";
	}
	return 0;
}

See my java code it’s giving tle.I know what is wrong but my problem is why it gives me tle instead of wa as I mentioned earlier.

System.out is probably too slow, since you’re printing O(n^2) integers. Either fix your solution so the length is actually N or create some FastWriter (and then fix your solution)

But the same solution is running in just 0.06 in C++ and also System.out.print(x); isn’t like endl so it doesn’t even store it just prints like cout<<x;.

I’ve hacked Java solutions on Codeforces using System.out on 2s TL that just read in/print ~8 \cdot 10^5 integers in total, so I/O is what I believe is the culprit. A simple benchmark supports my point:

Sorry, that was println. But the same benchmark for just print takes 3.4 seconds.

thank you :smiley: