jayati

Median of Two Sorted Arrays

Sep 20th, 2022
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. class Solution {
  2. public:
  3. double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
  4.  
  5. vector<int> v;
  6. for(int i=0;i<nums1.size();i++)
  7. {
  8. v.push_back(nums1[i]);
  9. }
  10. for(int i=0;i<nums2.size();i++)
  11. {
  12. v.push_back(nums2[i]);
  13. }
  14. sort(v.begin(),v.end());
  15. double ans;
  16. int l=v.size();
  17. if(v.size()%2==0)
  18. {
  19. ans=(v[(l/2)-1]+v[l/2])/2.0;
  20. }
  21. else
  22. {
  23. ans=double(v[l/2]);
  24. }
  25. return ans;
  26.  
  27. }
  28. };
Advertisement
Add Comment
Please, Sign In to add comment