HackerRank 'Max Min' / 'Angry Children' Solution

Martin Kysel · March 1, 2015

Short Problem Definition:

Given a list of N integers, your task is to select K integers from the list such that its unfairness is minimized.

Max Min


time complexity is O(N\*log(N))

space complexity is O(N)


The unfairness is the distance between K elements in a sorted array.


if __name__ == '__main__':
    n = input()
    k = input()
    candies = [input() for _ in range(0,n)]
    min_diff = 1000000000
    ## Write code here to compute the answer using (n, k, candies)

    for i in xrange(n - k + 1):
        min_diff = min(min_diff, candies[i+k-1] - candies[i])
    print min_diff

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <limits>
using namespace std;

int main() {
    /* The code required to enter n,k, candies is provided*/

    int N, K, unfairness = std::numeric_limits<int>::max();
    cin >> N >> K;
    int candies[N];
    for (int i=0; i<N; i++)
        cin >> candies[i];
    sort(candies, candies + N);
    for (int i=0; i < N - K + 1; i++){
        unfairness = min(unfairness, candies[i+K-1] - candies[i]);
    cout << unfairness << "\n";
    return 0;

Twitter, Facebook

To learn more about solving Coding Challenges in Python, I recommend these courses: Educative.io Python Algorithms, Educative.io Python Coding Interview.