Namritaansh02's blog

By Namritaansh02, history, 3 years ago, In English

This is the problem. I am using sparse table and implementing it exactly as given in the editorial but I am getting, TLE on test 3. I have been stuck for 2 days now on this:-(

Can anyone point out the mistake? Thanks.

This is my submission : 124743771

Full text and comments »

  • Vote: I like it
  • +5
  • Vote: I do not like it

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();
}

Full text and comments »

  • Vote: I like it
  • +6
  • Vote: I do not like it