In this HackerRank Max-Min interview preparation kit problem You will be given a list of integers, arr, and a single integer k. You must create an array of length k from elements of arr such that its unfairness is minimized. 


HackerRank Max Min Interview preparation kit solution


Problem solution in Python programming.

#!/usr/bin/env python

import collections, sys


if __name__ == '__main__':
    N = int(sys.stdin.readline())
    K = int(sys.stdin.readline())
    x = sorted(int(sys.stdin.readline()) for _ in range(N))
    print(min(x[i + K - 1] - x[i] for i in range(0, N - K - 1)))


Problem solution in Java Programming.

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

    // Complete the maxMin function below.
    static int maxMin(int k, int[] arr) {
        int min=1000000000;
        Arrays.sort(arr);
        for(int i=0;i<arr.length-k+1;i++){
            min=Math.min(min,arr[i+k-1]-arr[i]);
        }
        return min;
    }
    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int n = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        int k = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        int[] arr = new int[n];

        for (int i = 0; i < n; i++) {
            int arrItem = scanner.nextInt();
            scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
            arr[i] = arrItem;
        }

        int result = maxMin(k, arr);

        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();

        bufferedWriter.close();

        scanner.close();
    }
}


Problem solution in C++ programming.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int arr[100010];

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int n,k;
    cin>>n>>k;
    for(int i=0;i<n;i++)cin>>arr[i];
    sort(arr,arr+n);
    int ans=1e9;
    for(int i=k-1;i<n;i++){
        ans=min(arr[i]-arr[i-k+1],ans);
    }
    cout<<ans<<endl;
    return 0;
}


Problem solution in C programming.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int compare2uints(const void * a, const void * b) {
	return ( *(unsigned int*)a - *(unsigned int*)b );
}

int main() {
	int n,k,i;
	unsigned int x[100001];
	unsigned int j,minunfair;
	
	if (scanf(" %d",&n) != 1) return 1;
	if (scanf(" %d",&k) != 1) return 1;
	for (i=0; i<n; i++) if (scanf(" %d",x+i) != 1) return 1;
	if (k==1) return 0;
	qsort(x, n, sizeof(int), compare2uints);
	minunfair = 0x7fffffff;
	i = 0;
	j = k-1;
	while (j<n) {
		if (x[j]-x[i]<minunfair) minunfair=x[j]-x[i];
		i++;
		j++;
	}
	printf("%u\n",minunfair);
	return 0;
}


Problem solution in JavaScript programming.

function processData(input) {
    var cases = input.trim().split("\n"),
        N = - -cases.shift(),
        K = - -cases.shift(),
        result;
    for (var i = 0; i < N; i++) {
        cases[i] = - -cases[i];
    }
    cases.sort(function(a,b){return a-b});
    result = cases[K-1] - cases[0];
    for (var i = K; i < N; i++) {
        result = Math.min(result, cases[i] - cases[i-K+1]);
    }
    console.log(result);
}

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});