Following solution is quite straightforward. Let me know if you need detailed exp.
We can achieve these complexities
Preprocessing = O(n)
for Query 1,O(n^1/2)
for Query 2, O(n^1/2)
Just make an update function and iterate over blocks and update their sum in O(1) which in worst case will go up to sqrt(N) if you iterate over all such blocks.Same goes with sum query.
So time complexity will be as mentioned by @gagan86nagpal
Divide whole arrays in root(n) blocks.
For each block, create an array of size k. This will hold the count of numbers in the respective block for a modulus residue class (%k).
Eg. Let block contains 1 2 3 4 6 and k = 3, your array will be {2, 2, 1}.
For query 2:
l,r will span for some complete blocks and at max 2 partial blocks (leftmost and righmost blocks). For partial blocks, you should update your arr[] naively. For complete box, just maintain a shift variable (this represents how your arr[] values will shift).
Now, For query 1:
l,r will again span. For a complete block, ans is arr[0 ± shiftForThisBlock] and for partial blocks use brute force.
For both queries, Complete blocks are atmax sqrt(n) and partial nodes will be less than sqrt(n).
This gives sqrt(n) complexity,