Advertisement
Spocoman

02. Longest Sequence

Oct 27th, 2023 (edited)
630
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     int length, index = 0, count = 1, maxCount = 0;
  7.     cin >> length;
  8.  
  9.     int* numbers = new int[length];
  10.  
  11.     for (int i = 0; i < length; i++) {
  12.         cin >> numbers[i];
  13.     }
  14.  
  15.     for (int i = 1; i <= length; i++) {
  16.         if (numbers[i - 1] != numbers[i]) {
  17.             if (count >= maxCount) {
  18.                 index = i - count;
  19.                 maxCount = count;
  20.             }
  21.             count = 1;
  22.         }
  23.         else {
  24.             count++;
  25.         }
  26.     }
  27.  
  28.     for (int i = index; i < index + maxCount; i++) {
  29.         cout << numbers[i] << ' ';
  30.     }
  31.     cout << endl;
  32.     return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement