You are given with an array of N integers A[0], A[1], … , A[N-1] and an integer k. Your task is to make all the elements of the given array equal in a minimum number of ‘moves’. All the elements of the array are non-negative.In each ‘move’ you may add or subtract k from any element of the array.

Print -1 if it is not possible to make all the elements equal in any number of moves.

**Input:**

The first line contains a positive integer T denoting the number of test cases. Each of the test case consists of 2 lines. The first line of each test case contains two integers N and k. The next line contains space separated sequence of N integers.

**Output:**

Output the minimum number of ‘operations’ for each case on a new line. As the results can be large, print your answer modulo 10^{9} + 7.

**Constraints:**

1 <= T <= 10^{3}

1 <= N <= 10^{6}

1 <= k <= 10^{3}

1 <= A[] <= 10^{3}

**Example:**

**Input:**

1

4 2

7 5 21 17

**Output:**

13

**Explanation:**

We can add k = 2 to A[1] once and subtract k = 2 from A[2] seven times and from A[3] five times.The resulting array would be- 7 7 7 7

Number of moves = 1 + 7 + 5 = 13.

Author: sajalagrawal

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

saiujwal13083 | 347 |

isiddhisingh | 252 |

Abhishek_Jadhav | 243 |

_uncle_sam_ | 221 |

c_ocoooo | 217 |

saiujwal13083 | 610 |

SumitSingh27 | 466 |

c_ocoooo | 456 |

rathiarpit29 | 379 |

NaveenKumarNakka | 370 |

blackshadows | 5362 |

Ibrahim Nash | 5242 |

akhayrutdinov | 5111 |

mb1973 | 4931 |

Quandray | 4598 |

Login to report an issue on this page.