#C5088. Find the Missing Number in a Consecutive Sequence

    ID: 48698 Type: Default 1000ms 256MiB

Find the Missing Number in a Consecutive Sequence

Find the Missing Number in a Consecutive Sequence

You are given a list of integers which represents a consecutive sequence with exactly one missing number. Your task is to find and output that missing number.

The list may not be sorted. It is guaranteed that the numbers form an arithmetic sequence with a common difference of 1, except that one number is missing.

You can derive the missing number using the formula for the sum of an arithmetic series:

\( S = \frac{(n+1)(a_1 + a_{n+1})}{2} \)

where \( n+1 \) is the total number of elements in the complete sequence, \( a_1 \) is the minimum element and \( a_{n+1} \) is the maximum element of the complete sequence. The difference between this total and the sum of the provided numbers gives the missing number.

inputFormat

The input is read from standard input (stdin) and has the following format:

  • The first line contains an integer n, which represents the number of elements in the given list.
  • The second line contains n space-separated integers representing the sequence with one missing number.

outputFormat

Output the missing number to standard output (stdout).

## sample
5
1 2 3 5 6
4