Let instability of the array be the following value: $\max\limits_{i=1}^{n}a_i-\min\limits_{i=1}^{n}a_i$.
You have to remove exactly one element from this array to minimize instability of the resulting $(n-1)-$ elements array. Your task is to calculate the minimum possible instability.
Input
The first line of the input contains one integer $n(2\le n\le 10^5)$- the number of elements in the array $a$.
The second line of the input contains $n$ integers $a_1,a_2,...,a_n(1\le a_i\le 10^5)$- elements of the array $a$.
Output
Print one integer- the minimum possible instability of the array if you have to remove exactly one element from the array $a$.
Examples
input
Copy
4 1 3 3 7
output
Copy
2
input
Copy
2 1 100000
output
Copy
0
#include<bits/stdc++.h> using namespace std; int n,a[101000]; int main(){ cin>>n; for(int i=0;i<n;i++) cin>>a[i]; sort(a,a+n); cout<<min(a[n-1]-a[1],a[n-2]-a[0]); }
Không có nhận xét nào:
Đăng nhận xét