import java.io.*;
import java.util.*;
import java.math.*;

public class Solution {
  
    // Instance variables
    private static BufferedReader dataStream;

    // Main program
    public static void main(String[] args) throws Exception{
        
        // New Data Stream Instance
        dataStream  = new BufferedReader(new InputStreamReader(System.in));
        
        // Parsing in Input
        int n = Integer.parseInt(dataStream.readLine());
        int k = Integer.parseInt(dataStream.readLine());
        int[] candies = new int[n];
        for(int i = 0;i < n;i++){
            candies[i] = Integer.parseInt(dataStream.readLine());
        }
        
        // New solution instance
        Solution soln = new Solution();
        int result = soln.getMinUnfairness(candies,k);
        
        // Printing the output to the console
        System.out.println(result);
        
    }
    
    /** Sliding window algorithm on sorted candy array that determines min unfairness
     * Complexity is O(n). Counting sort is done since N has an upper bound (finite). 
     * @param candies Unsorted array of candies
     * @returns minUnfairness
     */
    private int getMinUnfairness(int[] candies,int k){
        
        // Sorting the candies
        Arrays.sort(candies);
        
        // Sliding on the window and recording the minUnfairness on all possible
        // windows with the candies array
        int result = candies[k-1] - candies[0];
        for(int i = 1;i < candies.length-k;i++){
            int curUnfairness = candies[i+k-1] - candies[i];
            if(curUnfairness<result)
                result = curUnfairness;
        }
        
        return result;
    }   
    
}

@@ -76,19 +76,16 @@
 ution %7B%0A
-  %0A
     // I
@@ -102,16 +102,16 @@
 riables%0A
+
     priv
@@ -148,17 +148,21 @@
 Stream;%0A
+    
 %0A
-
     // M
@@ -234,25 +234,16 @@
 eption%7B%0A
-        %0A
         
@@ -346,25 +346,16 @@
 m.in));%0A
-        %0A
         
@@ -630,33 +630,24 @@
 ;%0A        %7D%0A
-        %0A
         // N
@@ -762,25 +762,16 @@
 ies,k);%0A
-        %0A
         
@@ -844,25 +844,16 @@
 esult);%0A
-        %0A
     %7D%0A  
@@ -1169,25 +1169,16 @@
 int k)%7B%0A
-        %0A
         
@@ -1230,25 +1230,16 @@
 ndies);%0A
-        %0A
         
@@ -1602,17 +1602,8 @@
   %7D%0A
-        %0A
     
@@ -1621,16 +1621,16 @@
 result;%0A
+
     %7D   
@@ -1630,15 +1630,10 @@
    %7D   %0A
-    %0A
 %7D%0A

Update AngryChildren.java