fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int max(int a, int b){
  5. if (a > b) return a;
  6. return b;
  7. }
  8.  
  9. bool max_distance(int n, int t[]){
  10. int distance = 0;
  11. int backdistance = 0;
  12. for (int i = 0; i < n; i++) {
  13. if (t[i] != t[n - 1]) {
  14. distance = n - 1 - i;
  15. break;
  16. }
  17. }
  18. for (int i = n-1; i >= 0; i--) {
  19. if (t[i] != t[0]) {
  20. backdistance = n - 1 - i;
  21. break;
  22. }
  23. }
  24. int j = 0;
  25. for (int i = n-1; i >= 0; i--) {
  26. if (t[i] == t[0]){
  27. j++;
  28. }
  29. }
  30. if (j == n-1) return "BRAK";
  31. return max(distance,backdistance);
  32. }
  33.  
  34. int main() {
  35. int m;
  36. cin >> m;
  37. for (int i = 0; i < m; i++){
  38. int n;
  39. cin >> n;
  40. int t [n];
  41. for (int j = 0; j < n; j++){
  42. cin >> t[j];
  43. }
  44. cout << max_distance(n, t) << endl;
  45. }
  46. return 0;
  47. }
Success #stdin #stdout 0.01s 5320KB
stdin
3
6 
1 2 3 4 5 1
4 
1 1 1 1
3
1 4 1
stdout
1
0
1