A. Heist

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jj12345jj198999/article/details/82805983

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

There was an electronic store heist last night.

All keyboards which were in the store yesterday were numbered in ascending order from some integer number xx. For example, if x=4x=4and there were 33 keyboards in the store, then the devices had indices 44, 55 and 66, and if x=10x=10 and there were 77 of them then the keyboards had indices 1010, 1111, 1212, 1313, 1414, 1515 and 1616.

After the heist, only nn keyboards remain, and they have indices a1,a2,…,ana1,a2,…,an. Calculate the minimum possible number of keyboards that have been stolen. The staff remember neither xx nor the number of keyboards in the store before the heist.

Input

The first line contains single integer nn (1≤n≤1000)(1≤n≤1000) — the number of keyboards in the store that remained after the heist.

The second line contains nn distinct integers a1,a2,…,ana1,a2,…,an (1≤ai≤109)(1≤ai≤109) — the indices of the remaining keyboards. The integers aiai are given in arbitrary order and are pairwise distinct.

Output

Print the minimum possible number of keyboards that have been stolen if the staff remember neither xx nor the number of keyboards in the store before the heist.

Examples

input

Copy

4
10 13 12 8

output

Copy

2

input

Copy

5
7 5 6 4 8

output

Copy

0

Note

In the first example, if x=8x=8 then minimum number of stolen keyboards is equal to 22. The keyboards with indices 99 and 1111 were stolen during the heist.

In the second example, if x=4x=4 then nothing was stolen during the heist.

解题说明:此题需要找出最小数和最大数之间缺少的数的个数,模拟 排序后,直接从第二个以后开始遍历,判断相邻两个数之差是否等于1,不等于1表示缺的有物品。

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,a[1010],cnt=0;
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>a[i];
    sort(a,a+n);
    for(int i=1;i<n;i++)
        if(a[i]!=a[i-1]+1) cnt+=a[i]-a[i-1]-1;
    cout<<cnt<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/jj12345jj198999/article/details/82805983