We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Algorithms
- Greedy
- Max Min
- Discussions
Max Min
Max Min
Sort by
recency
|
879 Discussions
|
Please Login in order to post a comment
Two pointer approach
`java public static int maxMin(int k, List arr) { // Write your code here // Sorting and Sliding Window Collections.sort(arr); int min = 0; int end = 0; int start = 0; int minUnfairness = Integer.MAX_VALUE; while(end < arr.size()) { if((end-start)+1 < k) { end++; }else { int currentUnfairness = arr.get(end) - arr.get(start); minUnfairness = Math.min(minUnfairness, currentUnfairness); start++; } } return minUnfairness; }
`
Java