JSFiddle - React, Tailwind, and code Playground
by Diana Lemen
JavaScript
function maxMin(k, arr) {
for(let i = 0; i < arr.length; i++) {
for(let j = 0; j < arr.length; j++) {
if(arr[j] > arr[j + 1]) {
const cur = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = cur;
}
}
}
const minValues = arr.slice(0, k);
return Math.max(...minValues) - Math.min(...minValues);
}
console.log(maxMin(3, [100, 200, 300, 350, 400, 401, 402]))