Max Min Discussions | | HackerRank

Max Min

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