I dont understand why im getting this Runtime Error(OTHER) in this which is a solution to CodeChef: Practical coding for everyone.
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
ll t;
cin>>t;
while(t--) {
ll n;
ll sum=0;
cin>>n;
if(n==1) {
cout<<"1\n";
continue;
}
vector<ll> arr(n);
for(auto &x : arr) {
cin>>x;
sum+=x;
}
vector<ll> pref(n),suf(n);
pref[0] = arr[0];
for(ll i=1;i<n;i++) {
pref[i] = __gcd(pref[i-1],arr[i]);
}
suf[n-1] = arr[n-1];
for(ll i=n-2;i>=0;i--) {
suf[i] = __gcd(suf[i+1],arr[i]);
}
ll ans = sum/pref[n-1];
for(ll i=0;i<n;i++) {
ll k = __gcd( i-1<0 ? 0:pref[i-1],i+1>=n ? 0:suf[i+1]);
ans = min(ans,1ll + (sum-arr[i])/k);
}
cout<<ans<<"\n";
}
}
The official solution I found on the codechef youtube channel is this and I tested it and it works.
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
ios::sync_with_stdio(false); cin.tie(0);
ll TC;
cin >> TC;
while (TC--)
{
ll n, sum=0; cin >> n;
vector<ll> arr(n);
for (auto &x : arr) {
cin >> x; sum += x;
}
if (n == 1) {
cout << "1\n";
continue;
}
vector<ll>pref(n), suf(n);
pref[0] = arr[0];
for (ll i = 1; i < n; i++)
pref[i] = __gcd(pref[i - 1], arr[i]);
suf[n - 1] = arr[n - 1];
for (ll i = n - 2; i >= 0; i--)
suf[i] = __gcd(suf[i + 1], arr[i]);
ll ans = sum / pref[n - 1];
for (ll i = 0; i < n; i++) {
ll k = __gcd((i - 1 < 0 ? 0 : pref[i - 1]), (i + 1 >= n ? 0 : suf[i + 1]));
ans = min(ans, 1ll + (sum - arr[i]) / k);
}
cout << ans << '\n';
}
return 0;
}
So like the only difference is that I have the n=1 edgecase written as soon as n is taken as input which makes sense to me because why waste memory. But this gives the error while the code with the n=1 if condition after making the vector is working fine.
this is a pic of the error im getting, please help