fork download
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int n, a[100005], pre[100005];
  6.  
  7. int main() {
  8. cin >> n;
  9. for (int i = 1; i <= n; i++)
  10. cin >> a[i];
  11.  
  12. int Ans = 1000000000;
  13.  
  14. for (int i = 1; i <= n; i++) {
  15. int x = pre[a[i]];
  16. if (x != 0) {
  17. /// x va i
  18. /// so huu cung chieu cao a[x] = a[i]
  19. int kc = i - x;
  20. Ans = min(Ans, kc);
  21. }
  22. pre[a[i]] = i;
  23. }
  24. if (Ans == 1000000000) cout << -1;
  25. else
  26. cout << Ans;
  27. }
  28.  
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
-1