昨天滨院acm集训队遭到了很严重的盗窃,万恶的小偷将同学们梦寐以求的签到题偷走了。在失窃后,命题组手中还有n道签到题,编号分别为a1, a2, … an。现在命题组要统计有多少道试题失窃从而重新给同学们制作可爱的签到题。已知签到题从小到大编号,并且编号是连号的。换句话说,如果有两道签到题编号分别为4,9,则4-9内所有数字均为签到题编号。现在,由于命题人很懒,想要出尽可能少的题目,所以要求给出最少有多少道签到题失窃。
ps:这是一道没有被偷走的签到题奥
Input
The first line contains single integer n (1≤n≤1000) — the number of questoins in the store that remained after the heist. The second line contains n distinct integers a1,a2,…,an (1≤ai≤10^9) — the indices of the remaining questoins. The integers ai are given in arbitrary order and are pairwise distinct.
Output
Print the minimum possible number of questoins that have been stolen
Examples
Input
4
10 13 12 8
Output
2
Input
5
7 5 6 4 8
Output
0
Note
In the first example, if x=8 then minimum number of stolen questoins is equal to 2. The questoins with indices 9 and 11 were stolen during the heist. In the second example, if x=4 then nothing was stolen during the heist.
简单的快排,轻松解决问题
代码:
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
sort(a,a+n);
int num=0;
for(int i=0;i<n-1;i++){
num+=a[i+1]-a[i]-1;
}
cout<<num;
return 0;
}