fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void solve(){
  5. int n; cin >> n;
  6. vector<int> a(n);
  7. for(int i = 0; i < n; i++) cin >> a[i];
  8. for(int i = 0; i < n-1; i++){
  9. if(abs(a[i]-a[i + 1]) <= 1){
  10. cout << 0 << '\n';
  11. return;
  12. }
  13. }
  14. if(n == 2){
  15. if(abs(a[0] - a[1]) <= 1) cout << 0 << '\n';
  16. else cout << -1 << '\n';
  17. return;
  18. }
  19. for(int i = 0; i < n - 1; i++){
  20. int mn = min(a[i], a[i + 1]);
  21. int mx = max(a[i], a[i + 1]);
  22. for(int j = 0; j < n && j != i && j != i + 1; j++){
  23. if(a[j] >= mn && a[j] <= mx){
  24. cout << 1 << '\n';
  25. return;
  26. }
  27. else if(abs(a[j] - mn) <= 1 || abs(a[j] - mx) <= 1){
  28. cout << 1 << '\n';
  29. return;
  30. }
  31. }
  32. }
  33. cout << -1 << '\n';
  34. return;
  35. }
  36.  
  37.  
  38. int main(){
  39. ios::sync_with_stdio(false);
  40. cin.tie(nullptr);
  41. int t = 1;
  42. cin >> t;
  43. while(t--) solve();
  44. }
Success #stdin #stdout 0.01s 5292KB
stdin
1
3
2 7 5
stdout
-1