Runtime Error(OTHER)

I dont understand why im getting this Runtime Error(OTHER) in this which is a solution to Contest Page | CodeChef.

#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

sorry if the code isnt readable, im not used to this platform yet

Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

Edit:

I think I can decipher it; consider the test input:

2
1
1000000000
1
1000000000

WARNING: may lock up your PC for a while if you try running it!

i fixed the formatting, thanks for linking that page. can you explain whats going wrong when in the n=1 case? i dont understand why the console is waiting for more input even after i enter 1.

You’ve got a reproducible testcase - get debugging!

As a hint: change your solution to print out n after reading it and run it with the testcase I’ve provided.

i got it thank you, i removed the flushing buffer part and realized that the input needs to match. I think that error came because A1 was not inputted after you enter n=1 so the input was not matching with what was needed for the question. please correct me if i am wrong

1 Like

Exactly correct :slight_smile:

1 Like