fork download
  1. #include <iostream>
  2. #include<vector>
  3. #include<unordered_map>
  4. using namespace std;
  5.  
  6. int main() {
  7. vector<int> arr={1,1,2,2,2,1};
  8. int dist=0;
  9. int maxDist=0;
  10. unordered_map<int,int> d;
  11. for(int i=0;i<arr.size();i++) {
  12. if(d.find(arr[i])!=d.end()) {
  13. dist=i-d[arr[i]];
  14. maxDist=max(maxDist,dist);
  15. }
  16. else {
  17. d[arr[i]]=i;
  18. }
  19. }
  20. cout<<dist<<endl;
  21. return 0;
  22. }
Success #stdin #stdout 0.01s 5312KB
stdin
Standard input is empty
stdout
5