0
Why am i getting runtime error?
#include<bits/stdc++.h> using namespace std; typedef long long ll; int main() {ios_base::sync_with_stdio(false); cin.tie(NULL); ll t; cin>>t; while(t--) { ll n; cin>>n; ll a[n]; for(ll ch=0;ch<n;ch++) {cin>>a[ch];} vector<ll> v; ll count=0; ll dif=a[1]-a[0]; for(ll i=0;i<n-1;i++) { if(a[i+1]-a[i]==dif) { count++;} else {dif=a[i+1]-a[i]; v.push_back(count); count=1;} } sort(v.begin(),v.end()); cout<<"Case #"<<t-(t-1)<<": "<<v[v.size()-1]+1<<endl; } return 0; }`Preformatted text`
5 Antworten
0
Ayush Pattnaik C++does not support dynamic raw arrays on the stack. Compiler must know the exactly size of array for memory allocation.
Simply change
ll a[n];
to
vector<ll> a(n);
can resolve it.
Edit: This could still cause seg fault in some cases, and ~ swim ~ had correct answer.
For example, consider case
1 2 3 will cause error;
and
1 2 4 6 8 will give incorrect answer 2, not 3.
0
Still getting the same error
0
what is the input and the error? Could you share it?
0
This is the first question of round e Google kickstart