Max Min Discussions | | HackerRank

Max Min

Sort by

recency

|

74 Discussions

|

  • + 0 comments

    c++

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

    I am ashamed how much time it took me to finally understand whats going on. I guess it is a good practice on how to gather the useful observations on what the questions is actually about.

  • + 0 comments

    These problems are poorly written.

  • + 0 comments

    Rust best solution

    If you’re looking for solutions to the 3-month preparation kit in either Python or Rust, you can find them below: my solutions

    fn max_min(k: i32, arr: &[i32]) -> i32 {
        //Time complexity: O(n*log(n))
        //Space complexity (ignoring input): O(n)
        let mut arr = arr.to_vec();
        let k = k as usize;
        arr.sort_unstable();
        let mut minimun_unfairness = arr[k - 1] - arr[0];
        for index in 1..(arr.len() - k + 1) {
            if minimun_unfairness > arr[k - 1 + index] - arr[index] {
                minimun_unfairness = arr[k - 1 + index] - arr[index];
            };
        }
        minimun_unfairness
    }
    
  • + 0 comments

    Python best solution

    If you’re looking for solutions to the 3-month preparation kit in either Python or Rust, you can find them below: my solutions

    def max_min(k, arr):
        #Time complexity: O(n*log(n))
        #Space complexity (ignoring input): O(1)
        arr.sort()
        minimum_unfairness = arr[k - 1] - arr[0]
        for index in range(1, len(arr) - k + 1):
            if minimum_unfairness > arr[index + k - 1] - arr[index]:
                minimum_unfairness = arr[index + k - 1] - arr[index]
    
        return minimum_unfairness