Max Min Discussions | | HackerRank

Max Min

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