Why am i getting runtime error in this code?

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

What test input is causing the runtime error, or what Problem are you trying to solve?

4
7
10 7 4 6 8 10 11
4
9 7 5 3
9
5 5 4 5 5 5 4 5 6
10
5 4 3 2 1 2 3 4 5 6

Output:
Case #1: 4

Runtime Error
SIGSEGV

1 Like

Google kickstart round e problem 1

[simon@simon-laptop][09:35:39]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh 
Compiling pattnaikayush4-blah.cpp
+ g++ -std=c++14 pattnaikayush4-blah.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
+ set +x
Successful
[simon@simon-laptop][09:35:49]
[~/devel/hackerrank/otherpeoples]>echo "4
> 7
> 10 7 4 6 8 10 11
> 4
> 9 7 5 3
> 9
> 5 5 4 5 5 5 4 5 6
> 10
> 5 4 3 2 1 2 3 4 5 6" | ./a.out
Case #1: 4
/usr/include/c++/7/debug/vector:417:
Error: attempt to subscript container with out-of-bounds index -1, but 
container only holds 0 elements.

Objects involved in the operation:
    sequence "this" @ 0x0x7ffcc4e093f0 {
      type = std::__debug::vector<long long, std::allocator<long long> >;
    }
Aborted (core dumped)
3 Likes

so what do i need to do?

Use the 100% reproducible testcase to diagnose and fix the error :slight_smile:

3 Likes