Max Min Discussions | Algorithms | HackerRank
  • + 0 comments

    Java

    public static int maxMin(int k, List<Integer> arr) {
        Collections.sort(arr);
        int min =Integer.MAX_VALUE;
        int cal;
        List<Integer> list;
        for(int i=0;i<=arr.size()-k;i++){
            list=arr.subList(i, i+k);
            cal=list.get(list.size()-1)-list.get(0);
            if(cal<min){
                min=cal;
              if(min==0){
                  return 0;
              }
            }
        }
        return min;
    }