fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8. int T;
  9. cin >> T;
  10.  
  11. while (T--) {
  12. int n;
  13. cin >> n;
  14.  
  15. vector<int> heights(n);
  16. for (int i = 0; i < n; ++i) {
  17. cin >> heights[i];
  18. }
  19.  
  20. int maxLength = 0;
  21.  
  22. // Try adjusting each possible subarray
  23. for (int i = 0; i < n; ++i) {
  24. for (int j = i; j < n; ++j) {
  25. int minHeight = *min_element(heights.begin() + i, heights.begin() + j + 1);
  26. int maxHeight = *max_element(heights.begin() + i, heights.begin() + j + 1);
  27.  
  28. // Check if it's possible to adjust the subarray by modifying it
  29. if (maxHeight - minHeight <= j - i) {
  30. maxLength = max(maxLength, j - i + 1);
  31. }
  32. }
  33. }
  34.  
  35. cout << maxLength << endl;
  36. }
  37.  
  38. return 0;
  39. }
  40.  
Success #stdin #stdout 0s 5320KB
stdin
3
5
3 6 2 3 1
6
1 1 4 5 1 4
10
7 8 3 5 6 1 2 4 9 10
stdout
3
6
10