Single testcase alone gives right answer, but wrong answer when with other testcases.

Правка en1, от Namritaansh02, 2021-08-02 00:07:48

I get right answer in my program when this is the input:

1
8
465 55 3 54 234 12 45 78

however when I input this testcase along with others like below:

4
5
1 5 2 4 6
4
8 2 5 10
2
1000 2000
8
465 55 3 54 234 12 45 78

It shows uninitialized value error for the last test case. This is the problem statement.

Can anyone help with this?

This is my program:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair

void solve(){
    ll n;
    cin>>n;
    ll a[n+1],b[n];
    cin>>a[1];
    if(n<=2) {cout<<n<<"\n"; return;}
    for(int i=2;i<=n;i++){
        cin>>a[i];
        b[i-1]=abs(a[i]-a[i-1]);
    }
    ll mx=1,temp=1,t=b[1];
    for(int i=2;i<n;i++){
        if(__gcd(t,b[i])>1 && i!=n-1){
            t=__gcd(t,b[i]);
            temp++;
        }
        if(__gcd(t,b[i])==1){
            t=b[i];
            mx=max(mx,temp);
            temp=1;
            continue;
        }
        if(__gcd(t,b[i])>1 && i==n-1){
            temp++;
            mx=max(mx,temp);
        }
    } 
    cout<<mx+1<<endl;
}

int main(){
    ll tt=1;
    cin>>tt;
    while(tt--) solve();
}

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en1 Английский Namritaansh02 2021-08-02 00:07:48 1385 Initial revision (published)