fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long int
  4.  
  5. int main() {
  6. ll t, n;
  7. cin>>t;
  8. while(t--){
  9. cin>>n;
  10. ll i,j,err=-1, a[n], b[n-1],p;
  11. for(i=0;i<n;i++)
  12. cin>>a[i];
  13.  
  14. for(i=1;i<n-1;i++){
  15. if(a[i]!= (a[i-1]+a[i+1])/2)
  16. {
  17. err = i;
  18. break;
  19. }
  20. }
  21. ll r[]={err,err-1,err+1};
  22. if(err ==-1){
  23. r[0]=0;r[1] = (n-1)/2;r[2] = n-1;
  24. }
  25. j=0;
  26. ll min = INT_MAX;
  27. while(j<3){
  28. p =0;
  29. bool flag= true;
  30. for(i=0;i<n;i++){
  31. if(i==r[j])
  32. continue;
  33. b[p]=a[i];
  34. p++;
  35. }
  36. for(i=1;i<n-2;i++){
  37. if(b[i]!= (b[i-1]+b[i+1])/2)
  38. flag=false;
  39. }
  40. if(flag==true && min>a[r[j]])
  41. min = a[r[j]];
  42. j++;
  43. }
  44. if(min== INT_MAX){
  45. cout<<"-1\n";
  46. }
  47. else{
  48. cout<<min<<"\n";
  49. }
  50.  
  51. }
  52. return 0;
  53. }
Success #stdin #stdout 0s 3472KB
stdin
1

5

2 4 5 6 8
stdout
2