Problem:
Score : 300 points

Problem Statement

Given are an integer X and an integer sequence of length N: p_1, \ldots, p_N.

Among the integers not contained in the sequence p_1, \ldots, p_N (not necessarily positive), find the integer nearest to X, that is, find the integer whose absolute difference with X is the minimum. If there are multiple such integers, report the smallest such integer.

Constraints

1 \leq X \leq 100

0 \leq N \leq 100

1 \leq p_i \leq 100

p_1, \ldots, p_N are all distinct.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X N
p_1 ... p_N

Output

Print the answer.

Sample Input 1

6 5
4 7 10 6 5

Sample Output 1

8

Among the integers not contained in the sequence 4, 7, 10, 6, 5, the one nearest to 6 is 8.

Sample Input 2

10 5
4 7 10 6 5

Sample Output 2

9

Among the integers not contained in the sequence 4, 7, 10, 6, 5, the ones nearest to 10 are 9 and 11. We should print the smaller one, 9.

Sample Input 3

100 0

Sample Output 3

100

When N = 0, the second line in the input will be empty. Also, as seen here, X itself can be the answer.

Solution in JavaScript (Node.js 12.16.1) (judge verdict: Accepted)

"use strict";

const main = (arg) => {
  const array = arg.trim().split("\n");
  const array_A = array[0].trim().split(" ");
  const X = parseInt(array_A[0]);
  const N = parseInt(array_A[1]);
  if (N == 0) {
    console.log(X);
    return;
  }
  let array_B = array[1].trim().split(" ");
  array_B.sort(function (a, b) {
    return parseInt(a) < parseInt(b) ? -1 : 1;
  });

  let min = array_B[0];
  let max = array_B[N - 1];
  let maxNumber = 0;
  let minNumber = 0;

  for (let i = X; i < max; i++) {
    let maxFlag = true;
    for (let j = 0; j < array_B.length; j++) {
      // console.log(array_B[j]);
      if (array_B[j] == i) {
        maxFlag = false;
      }
    }
    if (maxFlag) {
      maxNumber = i;
      break;
    }
  }
  if (maxNumber == 0) {
    maxNumber = parseInt(max) + 1;
  }

  for (let k = X; min <= k; k--) {
    let minFlag = true;
    for (let l = 0; l < array_B.length; l++) {
      if (array_B[l] == k) {
        minFlag = false;
      }
    }
    if (minFlag) {
      minNumber = k;
      break;
    }
  }
  if (minNumber == 0) {
    minNumber = parseInt(min) - 1;
  }

  let result = 0;
  let maxResult = maxNumber - X;
  let minResult = X - minNumber;

  if (maxResult == minResult) {
    console.log(minNumber);
  } else if (maxResult < minResult) {
    console.log(maxNumber);
  } else if (minResult < maxResult) {
    console.log(minNumber);
  }
};
main(require("fs").readFileSync("/dev/stdin", "utf8"));
// main("6 5\n4 7 10 6 5");
// main("6 0\n");
// https://atcoder.jp/contests/abc170/tasks/abc170_c