fork download
  1. #include <iostream>
  2. using namespace std;
  3. #include <bits/stdc++.h>
  4. #include<vector>
  5. int main() {
  6. // your code goes here
  7. vector<int>arr={5,6,7,8,10,4,3,2,1};
  8. int k=8;
  9. int n=arr.size();
  10. //int count=0;
  11. int max_length=0;
  12. unordered_map<int,int>map;
  13. for(int j=0;j<n;j++)
  14. {
  15.  
  16. if(map.find(k-arr[j])!=map.end())
  17. {
  18. int length=j-map[arr[j]]+1;
  19. max_length=max(length,max_length);
  20. }
  21. if(map.find(arr[j])==map.end())
  22. {
  23. map[arr[j]]=j;
  24. }
  25. }
  26. if(max_length>=2)
  27. cout<<max_length;
  28. else
  29. cout<<-1;
  30. return 0;
  31. }
Success #stdin #stdout 0s 5280KB
stdin
Standard input is empty
stdout
9