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 C# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.Collections;
using System.Collections.Generic;

class Program {

    static void Main (string[] args) {
        string[] strtemp1 = Console.ReadLine ().Split (' ');
        long X = long.Parse (strtemp1[0]);
        int N = int.Parse (strtemp1[1]);
        var parray = new long[N];
        string[] strtemp2 = Console.ReadLine ().Split (' ');
        for (int i = 0; i < N; i++) {
            parray[i] = long.Parse (strtemp2[i]);
        }

        QuickSortS2B (parray);
        long binaryreturn = BinarySearch (parray, X);

        if (binaryreturn == -1) {
            Console.WriteLine (X);
        } else {
            bool found = false;
            long absdif = 1;
            while (!found) {
                if (binaryreturn - absdif >= 0) {
                    if (parray[binaryreturn - absdif] != parray[binaryreturn] - absdif) {
                        found = true;
                        Console.WriteLine (parray[binaryreturn] - absdif);
                        break;
                    }
                } else {
                    found = true;
                    Console.WriteLine (parray[binaryreturn] - absdif);
                    break;
                }
                if (binaryreturn + absdif < parray.Length) {
                    if (parray[binaryreturn + absdif] != parray[binaryreturn] + absdif) {
                        found = true;
                        Console.WriteLine (parray[binaryreturn] + absdif);
                        break;
                    }
                } else {
                    found = true;
                    Console.WriteLine (parray[binaryreturn] + absdif);
                    break;
                }
                absdif++;
            }
        }
    }

    static void QuickSortS2B (long[] a) {
        QuickSortS2B (a, 0, a.Length - 1);
    }

    static void QuickSortS2B (long[] a, int left, int right) {
        if (left < right) {
            int i = left;
            int j = right;
            long tmp;
            long pivot = med3 (a[i], a[i + (j - i) / 2], a[j]);
            while (true) {
                while (a[i] < pivot) i++;
                while (pivot < a[j]) j--;
                if (i >= j) break;
                tmp = a[i];
                a[i] = a[j];
                a[j] = tmp;
                i++;
                j--;
            }
            QuickSortS2B (a, left, i - 1);
            QuickSortS2B (a, j + 1, right);
        }
    }

    static long med3 (long x, long y, long z) {
        if (x < y) {
            if (y < z) {
                return y;
            } else if (z < x) {
                return x;
            } else {
                return z;
            }
        } else {
            if (z < y) {
                return y;
            } else if (x < z) {
                return x;
            } else {
                return z;
            }
        }

    }

    static long BinarySearch (long[] array, long target) {
        long min = 0;
        long max = array.Length - 1;

        while (min <= max) {
            long mid = min + (max - min) / 2;
            if (target > array[mid]) {
                min = mid + 1;
            } else if (target < array[mid]) {
                max = mid - 1;
            } else {
                return mid;
            }
        }
        return -1;
    }
}

//dotnet run --project test