fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. int compute_max_length(const std::vector<int>& v, int k)
  5. {
  6. int res = 0;
  7. int length = 0;
  8. bool is_valid = false;
  9.  
  10. for (int e : v) {
  11. if (e == k) {
  12. is_valid = true;
  13. ++length;
  14. } else if (e < k) {
  15. ++length;
  16. } else {
  17. if (is_valid) {
  18. res += length;
  19. }
  20. is_valid = false;
  21. length = 0;
  22. }
  23. }
  24. if (is_valid) {
  25. res += length;
  26. }
  27.  
  28. return res;
  29. }
  30.  
  31. int main()
  32. {
  33. std::cout << compute_max_length({2,1,4,9,2,3,8,3,4}, 4) << std::endl;
  34. }
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
5