2

So there was this code which got accepted when i took its output in a vector

#include <iostream> #include<vector> using namespace std; int main(){ int t; cin >> t; while(t--){ vector<int>v; int n,k; cin >> n >> k; for(int i=0;i<n;i++){ int x; cin >> x; if(x%k==0) v.push_back(1); else v.push_back(0); } for(auto x:v) cout <<x <<""; cout << endl; } return 0; } 

but then there is this code gives Time Limit Exceeded error when i am direct printing it

#include <bits/stdc++.h> using namespace std; #define ll long long int int main() { int t; cin>>t; while(t--) { ll k,d; int n,i; cin>>n>>k; for(i=0;i<n;i++) { cin>>d; if(d%k==0) cout<<"1"; else cout<<"0"; } cout<<"\n"; } } 

Can you tell why ? (The contest is now over)Here is the question in case

Edit:1 I used int instead of long long as well as printf as well as cin.tie(NULL) stuff , but still to no avail

5
  • 2
    Console I/O is expensive. You should avoid putting it into busy loops if possible. I think that could be a huge performance hit. You're best off getting all input, processing, then printing all output in most cases since I/O in this case is probably your bottle-neck. Commented Jul 25, 2020 at 18:24
  • Probably irrelevant, but there are other difference in the code: you are using long long in the 2nd, and printing strings instead of ints. When comparing 2 pieces of code, try and remove all differences except what you're trying to compare. Commented Jul 25, 2020 at 18:28
  • 1
    ... and this is just the nth example of why if someone's goal is to learn C++, they won't learn anything from these worthless contests. In nearly all cases, like this one, the correct solution is requires knowledge of a particular programming trick. If you don't know what the trick is and attempt to code a brute-force approach, the program runs slow and fails for that reason. If you're trying to learn C++, you won't learn anything from meaningless online contest sites but only from a good C++ textbook. Commented Jul 25, 2020 at 18:32
  • @SamVarshavchik -- Unfortunately it seems that a lot of newbies are being absorbed into solving these questions because they have been told that they will get a lucrative job at a major software company if they can solve these questions and get some sort of "high rank" to show some recruiter.. That's the retort I seem to get when I ask them "what good are these tests?" That's probably also the reason why you see a lot of cries of desperation, and sometimes no effort in these questions -- gotta get those ranking points on the question site. Commented Jul 25, 2020 at 18:52
  • I don't know if it will provide a lucrative job or not but it definitely is a good practice ground for Google Code Jam or Kickstart or FB Hacker's cup Commented Jul 26, 2020 at 5:43

1 Answer 1

1

The implementation with the cout in the for loop body is going to bottle-beck on the cout output for sure, especially given a modulo operation is very cheap in contrast.

See below question as reference:

C++: Does cout statement makes code slower

Something like this would work better:

#include <bits/stdc++.h> #include <vector> using namespace std; #define ll long long int int main() { int t; cin>>t; while(t--) { ll k,d; int n,i; cin>>n>>k; std::vector<bool> r(n); for(i=0;i<n;i++) { cin>>d; if(d%k==0) r[i] = true; } for(auto i : r) cout<<(i ? '1' : '0')<<endl; cout<<"\n"; } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.