Max Min Discussions | | HackerRank

Max Min

Sort by

recency

|

69 Discussions

|

  • + 0 comments

    Python 3 solutions with and without early exit:

    def maxMin(k: int, arr: list[int]) -> int:
        arr.sort()
        offset = k - 1
        min_unfairness = arr[offset] - arr[0]
        for i in range(k, len(arr)):
            if min_unfairness := min(arr[i] - arr[i - offset], min_unfairness):
                continue
            return 0
        return min_unfairness
    
    
    def maxMin(k: int, arr: list[int]) -> int:
        k -= 1
        arr.sort()
        return min(arr[i] - arr[i - k] for i in range(k, len(arr)))
    
  • + 0 comments

    python 3

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

    My rust solution:

    fn maxMin(k: i32, arr: &mut [i32]) -> i32 {
        arr.sort();
        
        arr.windows(k as usize)
            .map(|window| window[(k as usize) -1] - window[0])
            .min()
            .unwrap()
    
  • + 0 comments
    def maxMin(k, arr):
        arr.sort()
        diff = []
        for i in range(len(arr)-k+1):
            diff.append(abs(arr[i]-arr[i+k-1]))
        return (min(diff))
    
  • + 0 comments

    def maxMin(k, arr):

    dif = []
    arr.sort()
    n = k - 1
    for i in range(len(arr) - k + 1):
        dif.append(arr[n + i] - arr[i])
    return min(dif)