We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#include<bits/stdc++.h>#define ll long long usingnamespacestd;ll__gcd(lla,llb){if(b==0)returna;return__gcd(b,a%b);}signedmain(){lln;cin>>n;lla[100000+1];for(lli=0;i<n;++i)cin>>a[i];sort(a,a+n);if(n==1){cout<<a[0]+1<<endl;return0;}assert(n>=2);llprefix[100000+1]{0},suffix[100000+1]{0};llgcd=0;for(lli=0;i<n;++i){gcd=__gcd(gcd,a[i]);prefix[i]=gcd;}gcd=0;for(lli=n-1;i>=0;--i){gcd=__gcd(gcd,a[i]);suffix[i]=gcd;}if(a[0]%suffix[1]){cout<<suffix[1]<<endl;}elseif(a[n-1]%prefix[n-2]){cout<<prefix[n-2]<<endl;}else{for(lli=1;i<n-1;++i){llpreGcd=__gcd(suffix[i+1],prefix[i-1]);if(a[i]%preGcd){cout<<preGcd<<endl;break;}}}return0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
The Chosen One
You are viewing a single comment's thread. Return to all comments →