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 smallest_length=1000;
  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. smallest_length=min(length,smallest_length);
  20. }
  21.  
  22. map[arr[j]]=j;
  23. }
  24. if(smallest_length>=2)
  25. cout<<smallest_length;
  26. else
  27. cout<<-1;
  28. return 0;
  29. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
7