Array Rotation[ARRROT] , April Lunchtime 2021 , Why WA for many users?

In given question : " ARRROOT"

I think most of the users are able to crack the logic (for each query answer will be double of previous sum for explaination see editorial ) , but main thing is that why WA

Because array contains negative values too , and we are just printing Negative sum % (10**9 +7) which will be -ve

for negative values modulus will be calculated as (-ve value + mod ) %mod

#define mod 1000000007

ll modadd(ll a, ll b,ll MOD = mod){
    return((a%MOD)+(b%MOD)+MOD)%MOD;
}

ll modsub(ll a, ll b,ll MOD = mod){
    return((a%MOD) - (b%MOD) + MOD)%MOD;
}

loop(ii,tt)
{
    
    lli n;
    cin>>n;
    lli a[n];
    scanarr(a,n);
    lli sum=0;
    loop(i,n)
        sum=modadd(sum , a[i]);
    lli q;
    cin>>q;
    while(q--)
    {
        lli x;
        cin>>x;
        sum = modadd(sum,sum);
        print(sum);
    }
    
}

" Solution: 45557419 | CodeChef"

2 Likes

python users had this by default :rofl:

Link : c++ - Modulo operator with negative values - Stack Overflow

hmmm … . . .but python is slow , and also no gurantee whether it will give AC verdict or some runtime :rofl:

True (but I think in this particular case it worked) :sweat_smile: