fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5.  
  6. int n, k;cin>>n>>k;
  7. vector<int> arr(n,0);
  8. for(int i = 0;i<n;i++){
  9. cin>>arr[i];
  10. }
  11.  
  12. //create a adjacency list of value by positions
  13. unordered_map<int, vector<int>>mpp;
  14. for(int i = 0;i<n;i++){
  15. mpp[arr[i]].push_back(i);
  16. }
  17.  
  18. int ans = 0;
  19. for (auto &p : mpp) {
  20. vector<int>& indexList = p.second;
  21. int size = indexList.size();
  22.  
  23. int i = 0;
  24. for (int j = 0; j < size; j++) {
  25. while (i <= j && (indexList[j] - indexList[i] - (j - i)) > k) {
  26. i++;
  27. }
  28. ans = max(ans, j - i + 1);
  29. }
  30. }
  31.  
  32. cout<<ans<<endl;
  33.  
  34. return 0;
  35. }
Success #stdin #stdout 0.01s 5320KB
stdin
6 2
4 4 4 2 4 4 
stdout
5