Advertisement
uzimane_

Наибольший подмассив, составленный из последовательных чисел

Jul 10th, 2020
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4.  
  5. using namespace std;
  6.  
  7.  
  8. int main()
  9. {
  10. freopen("input.txt", "r", stdin);
  11. freopen("output.txt", "w", stdout);
  12. int n, a, * arr, mem, p, flag;
  13. cin >> n;
  14. arr = new int[n];
  15. for (int j = 0; j < n; j++)
  16. {
  17. cin >> a;
  18. arr[j] = a;
  19. }
  20. p = 0;
  21. mem = 0;
  22. flag = 0;
  23. for (int i = 0; i < n; i++)
  24. {
  25. if (arr[i + 1] == (arr[i] + 1))
  26. {
  27. if (flag == 2)
  28. p = 0;
  29. p++;
  30. if (p > mem)
  31. mem = p;
  32. flag = 1;
  33. }
  34. else
  35. {
  36. if (arr[i + 1] == (arr[i] - 1))
  37. {
  38. if (flag == 1)
  39. p = 0;
  40. p++;
  41. if (p > mem)
  42. mem = p;
  43. flag = 2;
  44. }
  45. else p = 0;
  46. }
  47. }
  48. cout << mem + 1;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement