[HackerRank] Max Min - Java Solution

문제

  • https://www.hackerrank.com/challenges/angry-children/problem

Solution

public class Solution {

    // Complete the countTriplets function below.
        static int maxMin(int k, int[] arr) {
        
        Arrays.sort(arr);
        
        int answer = Integer.MAX_VALUE;
        
        for(int i=0; i<arr.length-k+1; i++) {
            int min = arr[i];
            int max = arr[i+k-1];
            
            answer = (answer > max-min) ? max-min : answer;
        }

        return answer;
    }

    // skipped code
}

Leave a comment