Heist
Hazrat Ali
All keyboards which were in the store yesterday were numbered in ascending order from some integer number xx. For example, if x=4x=4 and 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.
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.
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.
4
10 13 12 8
2
5
7 5 6 4 8
0
Solution
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
sort(a.begin(), a.end());
cout << a[n - 1] - a[0] + 1 - n << endl;
return 0;
}