Max Min Discussions | | HackerRank

Max Min

  • + 0 comments

    short c++ solution

    #define all(a) (a).begin(), (a).end()
    
    int maxMin(int k, vector<int> arr) {
        sort(all(arr));
        
        int n = arr.size();
        int best = 1e9;
        for (int i = 0; i <= n-k; i++) {
            best = min(best, arr[k-1+i]-arr[i]);
        }
        return best;
    }