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`

23rd Aug 2020, 8:33 AM
Ayush Pattnaik
Ayush Pattnaik - avatar
4 Respostas
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.
23rd Aug 2020, 8:41 AM
Esch
Esch - avatar
0
Still getting the same error
23rd Aug 2020, 8:45 AM
Ayush Pattnaik
Ayush Pattnaik - avatar
0
what is the input and the error? Could you share it?
23rd Aug 2020, 8:55 AM
Esch
Esch - avatar
0
This is the first question of round e Google kickstart
23rd Aug 2020, 11:23 AM
Ayush Pattnaik
Ayush Pattnaik - avatar