Max Min Discussions | | HackerRank

Max Min

Sort by

recency

|

184 Discussions

|

  • + 0 comments

    Kotlin Solution

    fun maxMin(k: Int, arr: Array<Int>): Int {
        arr.sort()
        return (0..arr.size - k).minOf { i -> arr[i + k - 1] - arr[i] }
    }
    
  • + 0 comments

    c++

    int maxMin(int k, vector<int> arr) {
        int res = INT_MAX;
        std::sort(arr.begin(), arr.end());
        for(int i = 0; i + k -1 < arr.size(); ++i){
            res = std::min(res, arr[i+k-1] - arr[i]);
        }
        return res;
    }
    
  • + 0 comments

    short answer no need to loop. just sort input and pickup max-min is result. but instruction is test all pairs and it's need loop to follow instuction.

  • + 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;
    }
    
  • + 0 comments

    include

    include

    include

    include

    include

    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>arr[i]; sort(arr,arr+n); int ans=1e9; for(int i=k-1;i