We need to maximize the mex of the array.
Remember that the mex is the first non-negative integer that is missing. So if the final mex is at least m, then the final array must contain all numbers
0,1,2,…,m−1.
Therefore, instead of trying to directly maximize the mex, we can check numbers from small to large and see whether we can create them one by one.
The operation is simple: we can replace some value ai by ai+k. If we apply this operation many times, the value can become
ai,ai+k,ai+2k,….
So an element with initial value ai can become a number x if and only if both of these are true:
- x≥ai
- x≡ai(modk)
This means every element keeps the same remainder modulo k forever.