You are given an array of n integer numbers a0, a1, ..., an - 1. Find the distance between two closest (nearest) minimums in it. It is guaranteed that in the array a minimum occurs at least two times.
Input
The first line contains positive integer n (2 ≤ n ≤ 105) — size of the given array. The second line contains n integers a0, a1, ..., an - 1 (1 ≤ ai ≤ 109) — elements of the array. It is guaranteed that in the array a minimum occurs at least two times.
Output
Print the only number — distance between two nearest minimums in the array.
Examples
Input
Copy
2 3 3
Output
1
Input
Copy
3 5 6 5
Output
2
Input
Copy
9 2 1 3 5 4 1 2 3 1
Output
3
题意:在所给数组里找到两个最小数字的最小距离;
思路:时间2秒,可以放心暴力,先找到最小值,再求最小距离
#include<bits/stdc++.h>
#define Inf 0x3f3f3f3f
using namespace std;
const int N = 100005;
int main(){
int n,arr[N];
scanf("%d",&n);
int mint=Inf,index=0;
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
if(arr[i]<mint){
mint=arr[i];//最小值
index=i; //最小值第一次出现的位置
}
}
int l=n,per=index;
for(int i=index+1;i<n;i++){
if(arr[i]==mint){
l=min(l,i-per); //求最小距离
per=i;
}
}
printf("%d\n",l);
return 0;
}