I need help regarding weird time limit exceeded error in cpp - c++

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

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";
}
}

Related

Why this code is returning wrong value? inputs= 5 ,{ -1,4,-6,7,-4}

#include<iostream>
#include<climits>
using namespace std;
int main(){
int n;
cin>>n;
int a[n];
for(int i=0;i<n;++i){
cin>>a[n];
} //array instillisation
int cursum=0;
int maxsum=INT_MIN;
for(int i=0;i<n;++i){
cursum+=a[i];
if(cursum<0){
cursum=0;
}
maxsum=max(cursum,maxsum);
}
cout<<maxsum<<endl;
return 0;
}
//this code is for maximum subarray problem using kadane's algo.My compiler is retrurning wrong output
In cin you are doing incorrect operation it should be
cin>> a[i];
what you are doing is taking the value of a[n]

codeforces problem named Strange List the "MEMORY LIMIT EXCEEDED"

With reference to the problem named "Strange List" in codeforces following is my code
But the problem is All test case is passing but the last test case it is showing"MEMORY EXCEEDED LIMIT" and the last test case value is also huge like for n it is 100000.
Please guide me the way to tackle it
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
ll ele,sum=0,i,j,h,n,x;
cin>>n>>x;
vector <ll> vec;
for(i=0;i<n;i++)
{
cin>>ele;
vec.push_back(ele);
}
for(i=0;i>=0;i++)
{
if(vec.at(i)%x==1)
{
break;
}
else
{
h=vec.at(i)/x;
for(j=0;j<x;j++)
{
vec.push_back(h);
}
}
}
for(i=0;i<vec.size();i++)
{
sum+=vec.at(i);
}
cout<<sum<<"\n";
}
}
You can use Dynamic Programming to solve this problem
Implementation of storing results can be done by using Vector of pairs
Below is the AC code demonstrates how these kinds of problems can be done efficiently in terms of memory consumption......Happy Coding :)
CODE
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
ll t;
cin>>t;
while(t--){
ll n,x;
cin>>n>>x;
vector<pair<ll,ll>> a(n);
for(int i=0;i<n;i++){
cin>>a[i].first;
a[i].second=a[i].first; // Storing initial results in second part of pair
}
ll s=0;
ll d=0;
ll r=0;
ll k=0;
while(++r){
for(int i=0;i<n;i++){
if(a[i].first%x==0){
s+=a[i].second;
a[i].first=a[i].first/x;
a[i].second=(a[i].first*pow(x,r)); // updating results
}
else{
s+=a[i].second;
if(d==0)
k=i;
d=1;
}
}
if(d)
break;
}
for(int i=0;i<k;i++)
s+=a[i].second; // getting desired sum
cout<<s<<"\n"; // printing answer
}
}

why does it shows error even though it seems to be quite appropriate?

#include <iostream>
using namespace std;
int factors(int);
int main() {
int n;
cout<<"Enter a number: ";
cin>>n;
factors(n);
return 0;
}
factors(int n){
//To find out factors
int i, arrA[5];
arrA[0]=1;
cout<<"Factors: ";
for(int i=1;i<=n;i++){
for(int j=2;j<=n;j++){
if(n%j==0){
arrA[i]=j;
}
else if(n%j!=0){
i--;
}
}
}
for(int z=0;z < (sizeof(arrA)/sizeof(arrA[0]));z++){
cout<<arrA[z]<<" ";
}
}
This is a c++ code and for finding the factors of a given number.
But the problem is it's not producing the desired output.Problem is with the processing part(I think). It shows only "factors: " and continues to process and gives an error stating - "Untitled.exe has stopped working". Please fix the error and provide an elaborate explanation.
Clearly the problem comes from the for(int i=1;i<=n;i++){ that contains i-- which is a risk for infinite loop.
Since you tagged c++, do it with c++ this avoids a static array (arrA[5] is a good thing if you are sure there is only 5 factors but it depends on your n)
Instead use a std::list
#include <iostream>
#include <list>
using namespace std;
void factors(int n, std::list<int> & factors);
int main() {
int n;
cout<<"Enter a number: ";
cin>>n;
std::list<int> fact;
factors(n, fact);
cout<<"Factors: ";
std::list<int>::iterator it=fact.begin();
for(;it!=fact.end();it++)
{
std::cout << *it<<", ";
}
return 0;
}
void factors(int n, std::list<int> & factors)
{
for(int j=2;j<=n;j++){
if(n%j==0){
factors.push_back(j);
}
}
}

I got a RunTime error SIGCONT for the below code

#include<iostream>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int n,ctr=0;
cin>>n;
int a[n],ptr=750;
for(int i=0;i<n;i++){
cin>>a[i];
if(a[i]<ptr){
ctr++;
ptr=a[i];
}
}
cout<<ctr-(n/5)+1<<"\n";
}
return 0;
}
All other answers say I would have divided by zero or accessed out of bounds but here I do not find any such situation

SIGABRT error in my code in c++

I am getting SIGABRT error in my code. Can anyone tell me from where it might be coming?
Here's my code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
string s;
cin>>s;
int d=stoi(s,nullptr,2);
int f=__builtin_popcount(d);
cout<<f*(f-1)/2+f<<endl;
}
}
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long int t;
cin>>t;
while(t--)
{
long long int n;
cin>>n;
string s;
cin>>s;
long long int d=stoi(s,nullptr,2);
long long int f=__builtin_popcount(d);
cout<<f*(f-1)/2+f<<endl;
}`enter code here`
}
Now Try This