Max Min Discussions | | HackerRank

Max Min

Sort by

recency

|

181 Discussions

|

  • + 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

  • + 0 comments
    def maxMin(k, arr):
        x = sorted(arr)
        return min([x[i+k-1] - x[i] for i in range(len(arr) - k + 1)])
    
  • + 0 comments

    Kotlin

    fun maxMin(k: Int, arr: Array): Int { var sortedArray = arr.sorted() var result: Int? = null

    for(i in 0 until sortedArray.size - k - 1) {
        var diff = sortedArray[i + k - 1] - sortedArray[i]
        if(result == null) {
            result = diff
        } else {
            if(result > diff) {
                result = diff
            }
        }
    }
    return result ?: 0
    

    }

  • + 0 comments

    Javascript

    function maxMin(k: number, arr: number[]): number {
      let smallest = Number.POSITIVE_INFINITY;
      const sorted = arr.sort((a,b) => a-b);
      
      for (let i=0; i <= sorted.length - k; i++) {
        smallest = Math.min(smallest, sorted[i + k - 1] - sorted[i]);
      }
      
      return smallest;
    }