JSFiddle - React, Tailwind, and code Playground

by Dmitry Ieremenko

JavaScript

const n = 20;
let c = [13, 1, 11, 10, 6];

if (c.length === n) {
  return 0;
}
let result = 0;
c = c.sort((a, b) => a - b);
console.log(n, c);
loop1:
for (let i = 0; i < n; i++) {
  //console.log(i);
  if (c.includes(i)) {
    //console.log('no');
    continue;
  }
  const range = {};
  loop2:
  for (let j = 0; j < c.length; j++) {
    if (i === c[j]) {
      continue;
    }
    if (i > c[j]) {
      range.left = c[j];
    }
    if (i < c[j]) {
      range.right = c[j];
      break loop2;
    }
  }
  //console.log(range);
  const args = [];
  if (range.left !== undefined) {
    args.push(range.left);
  }
  if (range.right !== undefined) {
    args.push(range.right);
  }
  //console.log(args);
  const res = Math.min(...args.map(arg => Math.abs(i - arg)));
  result = res > result ? res : result;
}
console.log(result);

return result;