Problem:
Score : 300 points

Problem Statement

In some other world, today is Christmas Eve.

There are N trees planted in Mr. Takaha's garden. The height of the i-th tree (1 \leq i \leq N) is h_i meters.

He decides to choose K trees from these trees and decorate them with electric lights. To make the scenery more beautiful, the heights of the decorated trees should be as close to each other as possible.

More specifically, let the height of the tallest decorated tree be h_{max} meters, and the height of the shortest decorated tree be h_{min} meters. The smaller the value h_{max} - h_{min} is, the better. What is the minimum possible value of h_{max} - h_{min}?

Constraints

2 \leq K < N \leq 10^5

1 \leq h_i \leq 10^9

h_i is an integer.

Input

Input is given from Standard Input in the following format:

N K
h_1
h_2
:
h_N

Output

Print the minimum possible value of h_{max} - h_{min}.

Sample Input 1

5 3
10
15
11
14
12

Sample Output 1

2

If we decorate the first, third and fifth trees, h_{max} = 12, h_{min} = 10 so h_{max} - h_{min} = 2. This is optimal.

Sample Input 2

5 3
5
7
5
7
7

Sample Output 2

0

If we decorate the second, fourth and fifth trees, h_{max} = 7, h_{min} = 7 so h_{max} - h_{min} = 0. This is optimal.

There are not too many trees in these sample inputs, but note that there can be at most one hundred thousand trees (we just can't put a sample with a hundred thousand lines here).

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)


import java.io.PrintWriter;
import java.util.*;

/**
 * Hello AtCoder.
 *
 * @author kaito0228
 */
public class Main {
  public static void main(String[] args) {
    try (PrintWriter out = new PrintWriter(System.out);
        Scanner sc = new Scanner(System.in)) {
      Task task = new Task();
      task.solve(sc, out);
      out.flush();

    } catch (Exception e) {
      // DO NOT USE
      e.printStackTrace();
    }
  }

  private static class Task {
    private void solve(Scanner sc, PrintWriter out) {
      // TODO
      int n = nint(sc);
      int k = nint(sc);

      List<Integer> list = getIntegerList(sc, n);

      Collections.sort(list);
      int ans = Integer.MAX_VALUE;
      for (int i = 0; i < list.size() - k + 1; i++) {
        int min = list.get(i);
        int max = list.get(i + k - 1);
        int sa = max - min;

        if (sa < ans) {
          ans = sa;
        }
      }

      out.println(ans);
    }

    // method
    /**
     * Returns an int. int値を受け取り、返します。
     *
     * @param sc Scanner
     * @return int
     */
    private static int nint(Scanner sc) {
      return Integer.parseInt(sc.next());
    }

    /**
     * Returns a long. long値を受け取り、返します。
     *
     * @param sc Scanner
     * @return long
     */
    private static long nlong(Scanner sc) {
      return Long.parseLong(sc.next());
    }

    /**
     * Returns a double. double値を受け取り、返します。
     *
     * @param sc Scanner
     * @return double
     */
    private static double ndouble(Scanner sc) {
      return Double.parseDouble(sc.next());
    }

    /**
     * Returns a String. 文字列を受け取り、返します。
     *
     * @param sc Scanner
     * @return String
     */
    private static String nstr(Scanner sc) {
      return sc.next();
    }

    /**
     * Returns a int array. int値を受け取り、配列にして返します。
     *
     * @param sc Scanner
     * @param length Array length
     * @return int array
     */
    private static int[] intLine(Scanner sc, int length) {
      int[] iLine = new int[length];
      for (int i = 0; i < length; i++) {
        iLine[i] = nint(sc);
      }
      return iLine;
    }

    /**
     * Returns a long array. long値を受け取り、配列にして返します。
     *
     * @param sc Scanner
     * @param length Array length
     * @return long array
     */
    private static long[] longLine(Scanner sc, int length) {
      long[] lLine = new long[length];
      for (int i = 0; i < length; i++) {
        lLine[i] = nlong(sc);
      }
      return lLine;
    }

    /**
     * Returns a String array. 文字列を受け取り、配列にして返します。
     *
     * @param sc Scanner
     * @param length Array length
     * @return String array
     */
    private static String[] strLine(Scanner sc, int length) {
      String[] strLine = new String[length];
      for (int i = 0; i < length; i++) {
        strLine[i] = nstr(sc);
      }
      return strLine;
    }

    /**
     * Returns the maximum value in the list. リスト内で最大値を返します。
     *
     * @param longList List
     * @return long maximum value
     */
    private static long maxFromList(List<Long> longList) {
      return longList.stream().max(Comparator.naturalOrder()).get();
    }

    /**
     * Returns the minimum value in the list. リスト内で最小値を返します。
     *
     * @param longList List
     * @return long minimum value
     */
    private static long minFromList(List<Long> longList) {
      return longList.stream().min(Comparator.naturalOrder()).get();
    }

    /**
     * Returns the sum of each digit. 各桁の和を返します。
     *
     * @param n int
     * @return sum of each digit
     */
    private static int sumDigits(int n) {
      int sum = 0;
      while (n != 0) {
        sum += n % 10;
        n /= 10;
      }
      return sum;
    }

    /**
     * Returns the sum of each digit. 各桁の和を返します。
     *
     * @param n long
     * @return sum of each digit
     */
    private static long sumDigits(long n) {
      long sum = 0;
      while (n != 0) {
        sum += n % 10;
        n /= 10;
      }
      return sum;
    }

    /**
     * Return a list of Integer. Integerのリストを返します。
     *
     * @param sc Scanner
     * @param size list size
     * @return list
     */
    private static List<Integer> getIntegerList(Scanner sc, int size) {
      List<Integer> list = new ArrayList<>();
      for (int i = 0; i < size; i++) {
        list.add(nint(sc));
      }
      return list;
    }

    /**
     * Return a list of Long. Longのリストを返します。
     *
     * @param sc Scanner
     * @param size list size
     * @return list
     */
    private static List<Long> getLongList(Scanner sc, int size) {
      List<Long> list = new ArrayList<>();
      for (int i = 0; i < size; i++) {
        list.add(nlong(sc));
      }
      return list;
    }

    /**
     * Returns the summation of list. リストの要素の合計値を返します。
     *
     * @param list List<Integer>
     * @return sum of list
     */
    private static int sumList(List<Integer> list) {
      int sum = 0;
      for (Integer n : list) {
        sum += n;
      }
      return sum;
    }

    /**
     * Returns the average of list. リストの要素の平均値を返します。
     *
     * @param list List<Integer>
     * @return average of list
     */
    private static double aveList(List<Integer> list) {
      return (double) sumList(list) / list.size();
    }

    /**
     * Returns the greatest common divisor of the two given values. 与えた2つの値の最大公約数を返します。
     *
     * @param m long
     * @param n long
     * @return greatest common divisor
     */
    private static long gcd(long m, long n) {
      if (m < n) return gcd(n, m);
      if (n == 0) return m;
      return gcd(n, m % n);
    }

    /**
     * Returns the least common multiple of the two given values. 与えた2つの値の最小公倍数を返します。
     *
     * @param m long
     * @param n long
     * @return least common multiple
     */
    private static long lcm(long m, long n) {
      return m * n / gcd(m, n);
    }

    /**
     * Returns the most frequent value in the list. リスト内で最頻出数値を返します。
     *
     * @param list List
     * @return most frequent value
     */
    private static Integer mode(List<Integer> list) {
      if (list.size() <= 0) {
        throw new IllegalArgumentException();
      }

      Map<Integer, Integer> modeMap = new HashMap<>();

      for (Integer x : list) {
        if (modeMap.containsKey(x)) {
          modeMap.put(x, modeMap.get(x) + 1);
        } else {
          modeMap.put(x, 1);
        }
      }

      int maxValue = 0;
      int maxKey = 0;

      for (Map.Entry<Integer, Integer> entry : modeMap.entrySet()) {
        if (maxValue < entry.getValue()) {
          maxValue = entry.getValue();
          maxKey = entry.getKey();
        }
      }

      return maxKey;
    }
  }
}