Namritaansh02's blog

By Namritaansh02, history, 3 years ago, In English

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();
}
  • Vote: I like it
  • +6
  • Vote: I do not like it

| Write comment?
»
3 years ago, # |
  Vote: I like it +3 Vote: I do not like it

The problem is this line if(n<=2) {cout<<n<<"\n"; return;}.

You have to read all the numbers before returning. Otherwise, the next test case will read previous values.

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

This is because you are returning value in case n<=2 without taking up the complete input so that input is taken up in the next test case.