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.
publicstaticintmaxMin(intk,List<Integer>arr){// Write your code herearr.sort(Comparator.naturalOrder());intresult=Integer.MAX_VALUE;for(inti=0;i<=arr.size()-k;i++){result=Math.min(result,arr.get(i+k-1)-arr.get(i));}returnresult;}
Java solution:
Kotlin Solution
c++
short answer no need to loop. just sort input and pickup max-min is result. but instruction is test all pairs and it's need loop to follow instuction.
short c++ solution