You are given an array aa consisting of nn integer numbers.
Let instability of the array be the following value: maxi=1nai−mini=1naimaxi=1nai−mini=1nai.
You have to remove exactly one element from this array to minimize instability of the resulting (n−1)(n−1)-elements array. Your task is to calculate the minimum possible instability.
Input
The first line of the input contains one integer nn (2≤n≤1052≤n≤105) — the number of elements in the array aa.
The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1051≤ai≤105) — elements of the array aa.
Output
Print one integer — the minimum possible instability of the array if you have to remove exactly one element from the array aa.
Examples
input
4
1 3 3 7
output
2
2
1 100000
output
0
Note
In the first example you can remove 77 then instability of the remaining array will be 3−1=23−1=2.
In the second example you can remove either 11 or 100000100000 then instability of the remaining array will be 100000−100000=0100000−100000=0 and 1−1=01−1=0 correspondingly.
题目描述:
一个数组,去掉一个数后使它最大值减最小值尽可能的小。
分析:
排个序,讨论一下1,2,3个数时候的情况。再比较,去掉最小的和去掉最大的那个能让他最大值减最小值尽可能的小。
代码:
#include<stdio.h> #include<algorithm> #include<iostream> #include<queue> #define MIN(x,y) x<y?x:y; using namespace std; int main() { int n; cin>>n; int a[n+6]; for(int i=0;i<n;i++) { cin>>a[i]; } sort(a,a+n); if(n==2) cout<<0; else if(n==3) { if(a[0]==a[1]||a[1]==a[2]) cout<<0; else { if(a[1]-a[0]<a[2]-a[1]) cout<<a[1]-a[0]; else cout<<a[2]-a[1]; } } else { if(a[n-2]-a[0]<a[n-1]-a[1]) cout<<a[n-2]-a[0]; else cout<<a[n-1]-a[1]; } return 0; }