Dcoder10M's blog

By Dcoder10M, history, 5 hours ago, In English

Can someone point out the error in this code? It is giving the wrong answer on test case 6.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int mod = 1e9 + 7;

struct stack{
    vector<ll> st,hcf={0};

    void push(ll ele){
        st.push_back(ele);
        hcf.push_back(__gcd(hcf.back(),ele));
    }

    ll pop(){
        ll ret=st.back();
        st.pop_back();
        hcf.pop_back();
        return ret;
    }

    bool isEmpty(){
        return st.size()==0;
    }

    ll gcd(){
        return hcf.back();
    }
};

ll ans=INT_MAX;

::stack st1;
::stack st2;

void add(ll ele){
    st2.push(ele);
}

void remove(){
    if(st1.isEmpty()){
        while(!st2.isEmpty()){
            st1.push(st2.pop());
        }
    }
    st1.pop();
}

bool good(){
    if(__gcd(st1.gcd(),st2.gcd())==1){
        return true;
    }
    return false;
}

void solve(){
    ll n;cin>>n;
    vector<ll> arr(n);
    for(int i=0;i<n;i++){
        cin>>arr[i];
    }
    ll l=0;
    // cout<<ans<<endl;
    for(ll r=0;r<n;r++){
        add(arr[r]);
        while(good()){
            remove();
            ans=min(ans,r-l+1);
            l++;
        }
    }
    cout<<ans;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t=1;
    // cin>>t;
    while(t--){
        solve();
    }
    return 0;
}
  • Vote: I like it
  • 0
  • Vote: I do not like it