How do I calculate mod sum efficiently with query and updates

104 views Asked by At

Given an array A(say A[1..n]={1,2,3}) now I want two queries:

1) Update(idx,val) : update value of A[idx]=val

2) int Query(MOD).....

int Query(int MOD)  :   // say MOD =2 , so 1%2 +2%2 + 3%2 =2
    int ans=0;
    for i=1 to i=n
        ans+=(A[i]%MOD);
    return ans;

I thought of Fenwick Tree with indices as all possible values of MOD but the problem is I don't have constant updates because A[i]%MOD can be different for each A[i]? How Do I Do it efficiently?

0

There are 0 answers