fork download
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. vector<pair<int,int>> mergeArrays(vector<pair<int,int>> a,vector<pair<int,int>> b)
  6.  
  7. {
  8. vector<pair<int,int>> ans;
  9. int aSize = a.size();
  10. int bSize = b.size();
  11. int i=0;
  12. int j=0;
  13. while(i<a.size() && j <b.size())
  14. {
  15. if(a[i].first==b[j].first)
  16. {
  17. i++;
  18. j++;
  19. ans.push_back({a[i].first,(a[i].second+b[j].second)});
  20. }
  21. if(a[i].first < b[j].first)
  22. {
  23. ans.push_back({a[i].first,(a[i].second+b[j].second)});
  24. i++;
  25. }
  26. if(a[i].first > b[j].first)
  27. {
  28. ans.push_back({b[j].first,(a[i].second+b[j].second)});
  29. j++;
  30. }
  31.  
  32. }
  33. while(i<aSize)
  34. {
  35. ans.push_back({a[i].first,a[i].second});
  36. i++;
  37. }
  38. while(j<bSize)
  39. {
  40. ans.push_back({b[j].first,b[j].second});
  41. j++;
  42. }
  43.  
  44. return ans;
  45. }
  46.  
  47. int main() {
  48. vector<pair<int,int>> a = {{1,3},{2,4}};
  49. vector<pair<int,int>> b = {{12,14}};
  50. vector<pair<int,int>> c = mergeArrays (a,b);
  51. for(auto d : c)
  52. {
  53. cout<<d.first<<","<<d.second;
  54. cout<<"\n";
  55. }
  56. // your code goes here
  57. return 0;
  58. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
1,17
2,18
12,14