#import <Foundation/Foundation.h>
#import <stdlib.h>
#import <stdarg.h>
typedef NSInteger Int;
#ifndef _OS_OSTYPES_H
typedef NSUInteger UInt;
#endif
@interface NSString (Extension)
-(NSMutableArray*)componentsSeparatedByCharacter:(unichar)separator;
-(NSMutableArray*)numbersSeparatedByString:(NSString*)separator;
-(NSMutableArray*)numbersSeparatedByCharacter:(unichar)separator;
-(NSMutableArray*)splitCharacterEvery:(UInt)number;
-(NSMutableArray*)characterStrings; // 1文字ずつの NSString* の配列を返す
-(NSMutableArray*)characters; // 1文字ずつの unichar をラップした NSNumber の配列を返す
-(unichar)firstCharacter;
-(unichar)lastCharacter;
-(NSString*)leadingSubstringWithLength:(UInt)length;
-(NSString*)trailingSubstringWithLength:(UInt)length;
@end
@interface NSMutableArray (Extension)
+(NSMutableArray*)arrayWithInitialValue:(id)value length:(UInt)length; // 全部が value の値を持つ長さ length のオブジェクトを生成
+(NSMutableArray*)arrayWithNumbers:(Int)count, ...; // 要素数, 数1, 数2, ... から NSMutableArray<NSNumber*> オブジェクトを生成
-(NSMutableArray*)copy;
-(NSMutableArray*)unshift:(id)element;
-(id)shift;
-(NSMutableArray*)push:(id)element;
-(id)pop;
-(void)setInteger:(Int)value atIndex:(UInt)index;
@end
@interface NSArray (Extension)
+(instancetype)arrayWithRange:(NSRange)range;
-(Int)integerAtIndex:(UInt)index;
-(Int)firstInteger;
-(Int)secondInteger;
-(Int)thirdInteger;
-(Int)fourthInteger;
-(double)doubleAtIndex:(UInt)index;
-(NSString*)stringAtIndex:(UInt)index;
-(NSMutableArray*)sortedArrayWithAscending:(BOOL)ascending; // NSNumber* を要素に持つ配列をソート
-(NSMutableArray*)quickSortedArrayWithAscending:(BOOL)ascending; // Int をラップした NSNumber* を要素に持つ配列をクイックソート
-(NSMutableArray*)unique;
@end
@interface StdIO : NSObject
+(NSMutableString*)stringFromStdIn;
+(NSMutableArray*)stringsFromStdInSeparatedByString:(NSString*)separator;
+(NSMutableArray*)stringsFromStdInSeparatedByCharacter:(unichar)separator;
+(NSMutableArray*)numbersFromStdInSeparatedByString:(NSString*)separator;
+(NSMutableArray*)numbersFromStdInSeparatedByCharacter:(unichar)separator;
+(void)put:(char*)format, ...;
+(void)puts:(NSString*)format, ...;
@end
@interface NSMutableString (Extension)
-(NSMutableString*)unshift:(NSString*)string;
-(NSString*)shiftWithLength:(UInt)length;
-(NSMutableString*)push:(NSString*)string;
-(NSString*)popWithLength:(UInt)length;
@end
@implementation NSMutableArray (Extension)
+(NSMutableArray*)arrayWithInitialValue:(id)value length:(UInt)length
{
    NSMutableArray *array = [NSMutableArray array];
    for (UInt i=0; i<length; i++) {
        [array push: value];
    }
    return array;
}
+(NSMutableArray*)arrayWithNumbers:(Int)count, ...
{
    va_list numbers;
    va_start(numbers, count);
    NSMutableArray *array = [NSMutableArray array];
    for (Int i = 0; i < count; i++) {
        [array push: [NSNumber numberWithInt:va_arg(numbers, int)]];
    }
    va_end(numbers);
    return array;
}
-(NSMutableArray*)copy
{
    return [NSMutableArray arrayWithArray: self];
}
-(NSMutableArray*)unshift:(id)element
{
    [self insertObject: element atIndex: 0];
    return self;
}
-(id)shift
{
    if ([self count] > 0) {
        id first = [self objectAtIndex: 0];
        [self removeObjectAtIndex: 0];
        return first;
    } else {
        return nil;
    }
}
-(NSMutableArray*)push:(id)element
{
    [self addObject: element];
    return self;
}
-(id)pop
{
    UInt count = [self count];
    if (count > 0) {
        id last = [self objectAtIndex: count - 1];
        [self removeLastObject];
        return last;
    } else {
        return nil;
    }
}
-(void)setInteger:(Int)value atIndex:(UInt)index
{
    [self replaceObjectAtIndex:index withObject:@(value)];
}
@end
@implementation NSString (Extension)
-(NSMutableArray*)componentsSeparatedByCharacter:(unichar)separator
{
    return [NSMutableArray arrayWithArray: [self componentsSeparatedByString:[NSString stringWithFormat:@"%c", separator]]];
}
-(NSMutableArray*)numbersSeparatedByString:(NSString*)separator
{
    NSArray *strings = [self componentsSeparatedByString: separator];
    NSMutableArray *numbers = [NSMutableArray array];
    for (NSString *string in strings) {
        if (![string isEqualToString:@""]) {
            [numbers addObject: [NSNumber numberWithInteger: [string integerValue]]];
        }
    }
    return numbers;
}
-(NSMutableArray*)numbersSeparatedByCharacter:(unichar)separator
{
    return [self numbersSeparatedByString:[NSString stringWithFormat:@"%c", separator]];
}
-(NSMutableArray*)splitCharacterEvery:(UInt)number
{
    if ([self length] <= number) {
        return [NSMutableArray arrayWithObject: self];
    }
    NSMutableArray  *mArray = [NSMutableArray array];
    NSMutableString *mStr   = [NSMutableString stringWithString: self];
    NSRange range = NSMakeRange(0, number);
    while ([mStr length] > 0) {
        if ([mStr length] < number) {
            [mArray addObject: [NSString stringWithString: mStr]];
            [mStr deleteCharactersInRange:NSMakeRange(0, [mStr length])];
        } else {
            [mArray addObject: [mStr substringWithRange: range]];
            [mStr deleteCharactersInRange: range];
        }
    }
    return mArray;
}
-(NSMutableArray*)characterStrings {
    return [self splitCharacterEvery: 1];
}
-(NSMutableArray*)characters {
    NSMutableArray *result = [NSMutableArray array];
    for (UInt i=0; i<[self length]; i++) {
        [result push: @([self characterAtIndex: i])];
    }
    return result;
}
-(unichar)firstCharacter
{
    return [self characterAtIndex: 0];
}
-(unichar)lastCharacter
{
    return [self characterAtIndex: [self length] - 1];
}
-(NSString*)leadingSubstringWithLength:(UInt)length
{
    if (length <= [self length]) {
        return [self substringWithRange: NSMakeRange(0, length)];
    } else {
        return nil;
    }
}
-(NSString*)trailingSubstringWithLength:(UInt)length
{
    UInt mylength = [self length];
    if (length <= mylength) {
        return [self substringWithRange: NSMakeRange(mylength - length, length)];
    } else {
        return nil;
    }
}
@end
@implementation NSArray (Extension)
+(instancetype)arrayWithRange:(NSRange)range
{
    NSMutableArray *result = [NSMutableArray array];
    for(UInt loc = range.location; loc< range.location + range.length; loc++) {
        [result push: @(loc)];
    }
    return result;
}
-(Int)integerAtIndex:(UInt)index
{
    return [[self objectAtIndex:index] integerValue];
}
-(Int)firstInteger
{
    return [self integerAtIndex: 0];
}
-(Int)secondInteger
{
    return [self integerAtIndex: 1];
}
-(Int)thirdInteger
{
    return [self integerAtIndex: 2];
}
-(Int)fourthInteger
{
    return [self integerAtIndex: 3];
}
-(double)doubleAtIndex:(UInt)index
{
    return [[self objectAtIndex:index] doubleValue];
}
-(NSString*)stringAtIndex:(UInt)index
{
    return (NSString*)[self objectAtIndex:index];
}
-(NSMutableArray*)sortedArrayWithAscending:(BOOL)ascending
{
    NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey: @"self" ascending: ascending];
    NSArray *descriptors = [NSArray arrayWithObject: descriptor];
    NSArray *sortedArray = [self sortedArrayUsingDescriptors: descriptors];
    return [NSMutableArray arrayWithArray: sortedArray];
}
-(NSMutableArray*)quickSortedArrayWithAscending:(BOOL)ascending
{
    // Time complexity: O(n^2).
    //  * First time arround is to separate the entire array.
    //  * Second time around should sort the rest of the array.
    // Space complexity: O(log n)  +1 for unsortedArray but the mutable copy never gets added to the autorelease pool.
    // All of the arrays that are not attached to the returnArray are released by a compiler pass at the end of the method.
    UInt count = [self count];
    if (count <= 1) {
        return [NSMutableArray arrayWithArray: self];
    }
    Int pivot = [[self objectAtIndex: (count/2)] integerValue];
    NSMutableArray *smallerThanArray = [NSMutableArray array];
    NSMutableArray *largerThanArray = [NSMutableArray array];
    NSMutableArray *pivotArray = [NSMutableArray array];
    [pivotArray addObject: @(pivot)];
    for (Int e = 0; e < count; e++) {
        Int num = [[self objectAtIndex:e] integerValue];
        if (num < pivot) {
            [smallerThanArray addObject: @(num)];
        } else if (num > pivot) {
            [largerThanArray addObject: @(num)];
            // To address the possibly duplicate that is defined in the pivot, in this case 4.
        } else if (e != (count/2) && pivot == num) {
            [pivotArray addObject: @(num)];
        }
    }
    NSMutableArray *returnArray = [NSMutableArray array];
    if (ascending) {
        [returnArray addObjectsFromArray: [smallerThanArray quickSortedArrayWithAscending: ascending]];
        [returnArray addObjectsFromArray: pivotArray];
        [returnArray addObjectsFromArray: [largerThanArray quickSortedArrayWithAscending: ascending]];
    } else {
        [returnArray addObjectsFromArray: [largerThanArray quickSortedArrayWithAscending: ascending]];
        [returnArray addObjectsFromArray: pivotArray];
        [returnArray addObjectsFromArray: [smallerThanArray quickSortedArrayWithAscending: ascending]];
    }
    return returnArray;
}
-(NSMutableArray*)unique
{
    NSMutableArray *result = [NSMutableArray array];
    NSEnumerator *enumerator = [self objectEnumerator];
    id object;
    while ((object = [enumerator nextObject])) {
        if (![result containsObject: object]) {
            [result push: object];
        }
    }
    return result;
}
@end
@implementation NSMutableString (Extension)
-(NSMutableString*)unshift:(NSString*)string
{
    [self insertString: string atIndex: 0];
    return self;
}
-(NSString*)shiftWithLength:(UInt)length
{
    if ([self length] >= length) {
        NSRange range = NSMakeRange(0, length);
        NSString *first = [self substringWithRange: range];
        [self deleteCharactersInRange: range];
        return first;
    } else {
        return nil;
    }
}
-(NSString*)push:(NSString*)string
{
    [self appendString: string];
    return self;
}
-(NSString*)popWithLength:(UInt)length
{
    UInt mylength = [self length];
    if (length <= mylength) {
        NSRange range = NSMakeRange(mylength - length, length);
        NSString *last = [self substringWithRange: range];
        [self deleteCharactersInRange: range];
        return last;
    } else {
        return nil;
    }
}
@end
@implementation StdIO
+(NSMutableString*)stringFromStdIn
{
    NSFileHandle *handle = [NSFileHandle fileHandleWithStandardInput];
    NSMutableString *result = [NSMutableString string];
    NSData *data = [handle availableData];
    while ([data length] > 0) {
        [result appendString:[[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]];
        data = [handle availableData];
    }
    if ([result lastCharacter] == '\n') {
        [result popWithLength: 1];
    }
    return result;
}
+(NSMutableArray*)stringsFromStdInSeparatedByString:(NSString*)separator
{
    return [NSMutableArray arrayWithArray: [[StdIO stringFromStdIn] componentsSeparatedByString: separator]];
}
+(NSMutableArray*)stringsFromStdInSeparatedByCharacter:(unichar)separator
{
    return [NSMutableArray arrayWithArray: [[StdIO stringFromStdIn] componentsSeparatedByCharacter: separator]];
}
+(NSMutableArray*)numbersFromStdInSeparatedByString:(NSString*)separator
{
    return [[StdIO stringFromStdIn] numbersSeparatedByString: separator];
}
+(NSMutableArray*)numbersFromStdInSeparatedByCharacter:(unichar)separator
{
    return [[StdIO stringFromStdIn] numbersSeparatedByCharacter: separator];
}
+(void)putsToStdOut:(NSString*)string
{
    NSFileHandle *fileHandle = [NSFileHandle fileHandleWithStandardOutput];
    [fileHandle writeData:[string dataUsingEncoding: NSUTF8StringEncoding]];
    [fileHandle writeData:[@"\n" dataUsingEncoding: NSUTF8StringEncoding]];
}
+(void)put:(char*)format, ...
{
    va_list arguments;
    va_start(arguments, format);
    NSString *string = [[NSString alloc] initWithFormat: [NSString stringWithUTF8String: format] arguments: arguments];
    [StdIO putsToStdOut: string];
    va_end(arguments);
}
+(void)puts:(NSString*)format, ...
{
    va_list arguments;
    va_start(arguments, format);
    NSString *string = [[NSString alloc] initWithFormat: format arguments: arguments];
    [StdIO putsToStdOut: string];
    va_end(arguments);
}
@end
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSMutableArray *lines = [StdIO stringsFromStdInSeparatedByCharacter:'\n'];
        NSArray *firstLine = [(NSString*)[lines shift] numbersSeparatedByCharacter:' '];
        Int N = [firstLine firstInteger];
        Int K = [firstLine secondInteger];
        NSMutableArray *numbers = [NSMutableArray array];
        for (NSString *str in lines) {
            [numbers push: @([str integerValue])];
        }
        numbers = [numbers quickSortedArrayWithAscending:YES];
        Int min = NSIntegerMax;
        for (Int i=0; i<=N-K; i++) {
            Int diff = [numbers integerAtIndex:i+K-1] - [numbers integerAtIndex:i];
            min = (min<diff) ? min : diff;
        }
        [StdIO put:"%ld", min];
    }
    return 0;
}


Measured memory_bytes: 179224