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 Java (OpenJDK 11.0.6) (judge verdict: Wrong Answer)

import java.util.*;
import java.io.*;

class Main{
    public static void main(String args[])throws IOException{
        try(BufferedReader br = new BufferedReader(new InputStreamReader(System.in))){
          String[] input = br.readLine().split(" ");
          String[] p = br.readLine().split(" ");

          int X = Integer.parseInt(input[0]);
          int N = Integer.parseInt(input[1]);

          int[] table = new int[101];

          if(N != 0){
            int[] str = new int[N];
            
            for(int i = 0; i < N; i++){
              str[i] = Integer.parseInt(p[i]);
            }
            
            for(int i = 0; i <=100; i++){
              table[i] = i;
            }
            
            for(int i = 0; i < N; i++){
                table[str[i]] = -1;
            }

            int result_i = 0;
            int result_j = 0;
            int count_i = 0;
            int count_j = 0;
            boolean i_flag = false;
            boolean j_flag = false;
            
            for(int i = X; i <= 100; i++){
              count_i++;
              if(table[i] != -1){
                result_i = i;
                i_flag = true;
                break;
              }
            }

            for(int j = X; j >=1; j--){
              count_j++;
              if(table[j] != -1){
                result_j = j;
                j_flag = true;
                break;
              }
            }
            
            if(i_flag && j_flag){
            if(count_i == count_j){
              System.out.println(table[result_j]);
            }else if(count_i > count_j){
              System.out.println(table[result_j]);
            }else if(count_i < count_j){
              System.out.println(table[result_i]);
            }
            }else if(j_flag){
                System.out.println(table[result_j]);
            }else if(i_flag){
                System.out.println(table[result_i]);
            }

          }else{
            System.out.println(X);
          }
        }

    }
}