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.
#using a sliding window on a reverse sorted arraydefmaxMin(k,arr):# Write your code heresorted_arr=sorted(arr,reverse=True)current_window=[]mini=max(arr)forendinrange(len(sorted_arr)):current_window.append(sorted_arr[end])iflen(current_window)==k:diff=current_window[0]-current_window[k-1]ifdiff<mini:mini=diffcurrent_window.pop(0)returnmini
C#
Collections.sort(arr); List temp = new ArrayList(); for(int i=0;i<=arr.size()-k;i++){ temp.add(Math.abs(arr.get(i+k-1)-arr.get(i))); }
Solution in TS
Solution in Python3 :)