Given an array of **n** integers and a positive number **k**. We are allowed to take any k integers from the given array. The task is to find the minimum possible value of the difference between maximum and minimum of **k** numbers.

**Input:**

The first line of input contains an integer T denoting the number of test cases. Each test case contains two integers n and k where n denotes the number of elements in the array a[]. Next line contains space separated n elements in the array a[].

**Output:**

Print an integer which denotes the minimum difference achieved.

**Constraints:**

1<=T<=50

1<=n<=1000

1<=a[i]<=100000

1<=k<=1000

**Example:
Input:**

2

7 3

10 100 300 200 1000 20 30

10 4

1 2 3 4 10 20 30 40 100 200

20

3

Author: shashwat jain

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.

narahc321 | 170 |

agness | 163 |

lonecoder | 153 |

2016uec1342 | 142 |

HuHu | 140 |

lonecoder | 1088 |

nikhil_sojan | 1082 |

KshatriyaYash | 1066 |

mazumderrohit8 | 928 |

SumitSingh27 | 832 |

blackshadows | 5327 |

Ibrahim Nash | 5215 |

akhayrutdinov | 5111 |

mb1973 | 4504 |

Quandray | 4444 |

Login to report an issue on this page.