Practice on Toph

Participate in exhilarating programming contests, solve unique algorithm and data structure challenges and be a part of an awesome community.

Help Chokro

By RUDRA_DAS · Limits 1s, 512 MB

Pias and Chokro are good friends. Pias's birthday is coming soon. So, Chokro wants to gift Pias an array on his birthday. Initially, Chokro has an array AA of size nn which has values A1,A2,...,AnA_{1}, A_{2}, ..., A_{n}.

Chokro can perform the following operations on this array any number of times (possibly zero):

  1. Ai=Ai+1A_{i} = A_{i} + 1.
  2. Ai=Ai1A_{i} = A_{i} - 1.

Chokro knows that Pias's lucky number is kk, and he likes an array of equal values. So, to make him more excited Chokro wants to convert the array such that A1=A2=...=AnA_{1} = A_{2} = ... = A_{n} and Ai%k=0 A_{i} \% k = 0 for all ii (1in)(1 \leq i \leq n).

As Chokro's online classes are starting soon he doesn't have much time. So, he thought that it would be better to write a code to know the minimum number of operations required to convert the array so that Pias likes that.

But Chokro is afraid of programming. So, he wants your help. Can you help Chokro?


First line will contain two integers nn, kk (1n,k1061 \leq n, k \leq 10^{6}) the size of the array and Pias's lucky number respectively.

Next line will contain n space separated integers A1A_{1}, A2A_{2}, ..., AnA_{n} (0Ai1060 \leq A_{i} \leq 10^{6}).


Output a single integer the minimum number of operations Chokro needs to convert the array.


5 3
1 8 7 6 4
6 2
3 5 7 12 9 0



62% Solution Ratio

TUNA_2023Earliest, Jul '20

Saima.153883Fastest, 0.1s

OttoTheDinoLightest, 4.1 MB

OttoTheDinoShortest, 203B


Login to submit

Toph uses cookies. By continuing you agree to our Cookie Policy.