Day - 23
Hello, guys. It is the 23rd day of our coding challenge. I am glad to share solutions everyday. And it is helping me improving my coding abilities. Even, sometimes I do not have enough time I am trying to solve at least one problem.
It is time to write a code.
/*
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var minimumDifference = function (nums, k) {
nums.sort((a, b) => a - b);
let left = 0;
let right = 0;
let minDifference = Number.MAX_VALUE;
while (left <= nums.length - k) {
while (right - left + 1 < k) {
right++;
}
minDifference = Math.min(
minDifference, nums[right] - nums[left]
);
left++;
}
return minDifference === Number.MAX_VALUE ? 0 : minDifference;
};
That's all what I am going to share with you. Thanks for your attention.