Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main () {
  6. int n, target;
  7. cin >> n >> target;
  8. vector <int> arr(n);
  9. for (int i = 0; i < n; i++) cin >> arr[i];
  10.  
  11. map <int, vector <int>> pos;
  12. for (int i = 0; i < n; i++) {
  13. pos[arr[i]].push_back(i);
  14. }
  15.  
  16. for (int x = 0; x < n; x++) {
  17. int k = target - arr[x];
  18. //a_y = k and y != x
  19. if (pos.count(k) == 1) {
  20. bool valid = false;
  21. int cnt = 0;
  22. for (int pos_y: pos[k]) {
  23. if (pos_y != x) {
  24. valid = true;
  25. }
  26. cnt++;
  27. if (cnt == 2) break;
  28. }
  29.  
  30. if (valid) {
  31. // tienes tu respuesta
  32. }
  33. }
  34. }
  35. return (0);
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement