fork download
  1. //-----------------------------defining section------------------------------------------------------------------------------------------------//
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. typedef long long ll;
  5. #define fl(i,l, h) for(int i = l; i < h; i++)
  6. #define fd(i,h,l) for(int i = h; i >= l; i--)
  7.  
  8. //-----------------------------functions section------------------------------------------------------------------------------------------------//
  9. struct node {
  10. int data;
  11. struct node* right;
  12. struct node* left;
  13. };
  14.  
  15. typedef struct node* NODE;
  16. NODE getnode(int x) {
  17. NODE temp = (NODE)malloc(sizeof(struct node));
  18. temp->data = x;
  19. temp->left = NULL;
  20. temp->right = NULL;
  21. return temp;
  22. }
  23.  
  24. void insert(NODE &root, int x) {
  25. if (root == NULL) {
  26. root = getnode(x);
  27. }
  28. if (x < root->data) {
  29. insert(root->left, x);
  30. }
  31. else if (x > root->data) {
  32. insert(root->right, x);
  33. }
  34.  
  35. }
  36. void create(NODE &root) {
  37.  
  38. int x;
  39. while (true) {
  40. cin >> x;
  41. if (x == -999) break;
  42. insert(root, x);
  43. }
  44. }
  45. void InOrder(NODE root, vector<int> &a) {
  46.  
  47. if (root == NULL) return;
  48. InOrder(root->left, a);
  49. a.push_back(root->data);
  50. InOrder(root->right, a);
  51.  
  52. }
  53. int main() {
  54. #ifndef ONLINE_JUDGE
  55. freopen("input.txt", "r", stdin);
  56. freopen("output.txt", "w", stdout);
  57. #endif
  58.  
  59. ios_base::sync_with_stdio(false);
  60. cin.tie(NULL);
  61. //-----------------------------input section------------------------------------------------------------------------------------------------//
  62.  
  63. int k;
  64. cin >> k;
  65.  
  66. //---------------------------------logic------------------------------------------------------------------------------------------------//
  67.  
  68. NODE root = NULL;
  69. create(root);
  70. vector<int> a;
  71. InOrder(root, a);
  72.  
  73.  
  74. int j = 0, maxLength = -1;
  75. fl(i,1,a.size()) {
  76. if (abs(a[i] - a[i - 1]) == k) {
  77. maxLength = max(maxLength, i - j + 1);
  78. }
  79. else {
  80. j = i;
  81. }
  82. }
  83.  
  84.  
  85. //-----------------------------------display section------------------------------------------------------------------------------------------------//
  86.  
  87.  
  88. cout << maxLength;
  89. return 0;
  90. }
Success #stdin #stdout 0s 5320KB
stdin
1 
5
4
3
6
7
-999
stdout
5