fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. vector<int> get_intersection(vector<int> &a, vector<int> &b) {
  6. int i = 0;
  7. int j = 0;
  8. vector<int> out;
  9.  
  10. while (i != a.size() && j != b.size()) {
  11. if (a[i] == b[j]) {
  12. out.push_back(a[i]);
  13. //if (i < a.size()) {
  14. i++;
  15. //}
  16. //if (j < a.size()) {
  17. j++;
  18. //}
  19. }
  20. else if (a[i] < b[j]) {
  21. //if (i < a.size()) {
  22. i++;
  23. //}
  24. }
  25. else if (a[i] > b[j]) {
  26. //if (j < b.size()) {
  27. j++;
  28. //}
  29. }
  30. }
  31.  
  32. return out;
  33. }
  34.  
  35. int main() {
  36. // your code goes here
  37.  
  38. vector<int> a = {1,4,7,10,13};
  39. vector<int> b = {1,3,5,5,7,13};
  40.  
  41. vector<int> out = get_intersection(a, b);
  42.  
  43. for (auto &e: out) {
  44. cout << e << " ";
  45. }
  46.  
  47. return 0;
  48. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
1 7 13