2817. Min Abs Diff Between Elms With Constraint

mail@pastecode.io avatar
unknown
typescript
a year ago
433 B
8
Indexable
function minAbsoluteDifference(nums: number[], x: number): number {
    if(x === 0) {
        return 0;
    }
    const n = nums.length;
    let res = Number.MAX_SAFE_INTEGER;
    for(let i = 0; i < n - x; i++) {
        for(let j = i + x; j < n; j++) {
            res = Math.min(res, Math.abs(nums[i] - nums[j]));
            if(res === 0) {
                return 0;
            }
        }
    }
    return res;
};