fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main(){
  4. int arr[6]={1,1,2,2,2,1};
  5. unordered_map<int,int>hmap;
  6. int diff=0;
  7. for(int i=0;i<6;i++){
  8. if(hmap.find(arr[i])==hmap.end()){
  9. hmap[arr[i]]=i;
  10. }
  11. else{
  12. diff=max(diff,i-hmap[arr[i]]);
  13. }
  14. }
  15. cout<< diff;
  16. }
Success #stdin #stdout 0.01s 5304KB
stdin
Standard input is empty
stdout
5