fork download
  1. #include<bits/stdc++.h>
  2. #define int long long
  3. #define ll long long
  4. #define FAST ios::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
  5. using namespace std;
  6. signed main()
  7. {
  8. FAST
  9. int n,i=0,mn=INT_MAX,mx=INT_MIN,min_idx,max_idx,ans=INT_MAX;cin>>n;
  10. vector<int>v(n);
  11. for(int i=0;i<n;i++){
  12. cin>>v[i];
  13. mn=min(mn,v[i]);
  14. mx=max(mx,v[i]);
  15. }
  16. for(int i=0;i<n;i++){
  17. if(v[i]==mn)min_idx=i+1;
  18. if(v[i]==mx)max_idx=i+1;
  19. ans=min(ans,abs(min_idx-max_idx));
  20. }
  21. cout<<ans;
  22. }
Success #stdin #stdout 0.01s 5300KB
stdin
4 
1 1 1 1
stdout
Standard output is empty