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.
`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;
}
`
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Max Min
You are viewing a single comment's thread. Return to all comments →
`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; }
`