fork download
  1. /******************************************************************************
  2.  
  3. Welcome to GDB Online.
  4. GDB online is an online compiler and debugger tool for C/C++.
  5. Code, Compile, Run and Debug online from anywhere in world.
  6.  
  7. *******************************************************************************/
  8. #include <iostream>
  9. #include <vector>
  10. using namespace std;
  11.  
  12. double medianOf2(vector<int>& a, vector<int>& b) {
  13. // Your code goes here
  14. int n = a.size();
  15. int m = b.size();
  16. vector<int> res(n+m);
  17. int first = 0, second = 0, ind = 0;
  18. double result;
  19. while(first < n && second < m)
  20. {
  21. if(a[first] < b[second])
  22. {
  23. res[ind] = a[first];
  24. ind++;
  25. first++;
  26. }
  27. else
  28. {
  29. res[ind] = b[second];
  30. ind++;
  31. second++;
  32. }
  33. }
  34. if((n+m)%2)
  35. {
  36. result = (res[(n+m)/2]);
  37. }
  38. else
  39. {
  40. result = ( (res[(n+m)/2] + res[((n+m)/2)-1]) / 2.0) ;
  41. }
  42. return result;
  43. }
  44.  
  45. int main()
  46. {
  47. vector<int> arr1 = { 1, 2};
  48. vector<int> arr2 = { 3, 4 };
  49.  
  50. cout << medianOf2(arr1, arr2) << endl;
  51. return 0;
  52. }
Success #stdin #stdout 0s 5320KB
stdin
45
stdout
1